84669 personnes étudient
152542 personnes étudient
20005 personnes étudient
5487 personnes étudient
7821 personnes étudient
359900 personnes étudient
3350 personnes étudient
180660 personnes étudient
48569 personnes étudient
18603 personnes étudient
40936 personnes étudient
1549 personnes étudient
1183 personnes étudient
32909 personnes étudient
class Animal: def __init__(self, animal): self.animal = animal def type(self, type=self.animal): print type
运行的时候出现 NameError: name 'self' is not defined?
NameError: name 'self' is not defined?
ringa_lee
方法参数的默认值是在函数定义的时候初始化的,而self指该class的实例化类,只有实例化之后才有值,因此这里编译出错(不是运行时错误)
如果打印的默认值非要设为self.animal的话,试试这样:
class Animal(object): def __init__(self,animal): self.animal = animal def type(self,type=None): print type if type else self.animal
你还需要了解一下self,在类中哪里可以访问得到self,哪里不可以!
方法参数的默认值是在函数定义的时候初始化的,而self指该class的实例化类,只有实例化之后才有值,因此这里编译出错(不是运行时错误)
如果打印的默认值非要设为self.animal的话,试试这样:
你还需要了解一下self,在类中哪里可以访问得到self,哪里不可以!