Discuss / Python / 如果不做判断 没任何意义吧

如果不做判断 没任何意义吧

Topic source

@unique

class Gender(Enum):

    Male   = 0

    Female = 1

    wew = 'ew'

class Student(object):

    def __init__(self, name, gender):

        if(type(name)!= str):

            print('name err')

        elif type(gender) != type(Gender.Male):#下一句为什么不行

        #elif (gender.value != Gender.Male.value) or (gender.value != Gender.Female.value):

             print('gender err')

        else:

            self.name = name

            self.gender = gender

    def prt(self):

        print('%s gender is %d'%(self.name,self.gender.value))

a = Student('ni',Gender.Male)

b = Student('hao',Gender(1))#Gender.Female)#测试

a.prt()

b.prt()

print(a.gender,a.gender.value)

稍微补充一下,大佬想的确实很透彻。另外请问wew='ew'是做什么用的呢?

class Student(object):    def __init__(self,name,gender):        if not isinstance(name,str):            raise TypeError('name must be a str')        elif not isinstance(gender,Gender):            raise TypeError('gender must be an enumerated type ')        else:            self.name=name            self.gender=gender    def print_stu(self):        print('%s\'s gender is %d' % (self.name,self.gender.value))a=Student('LiMing',Gender.Male)b=Student('Jenny',Gender.Female)a.print_stu()b.print_stu()#testbart=Student('Bart',Gender.Male)if bart.gender==Gender.Male:    print('Test successful')else:    print('Test failure')

wee纯粹手抽筋打上去的

__gentry

#4 Created at ... [Delete] [Delete and Lock User]

#下一句为什么不行:把or改成and


  • 1

Reply