报错原因,请大神指导
class Person(object):
def __int__(self,name,gender,birth):
self.name=name
self.gender=gender
self.birth=birth
p1=Person('Xiao Ming','Male','1991-1-1')
p2=Person('Xiao Hong','Female','1992-2-2')
print p1.name
为什么会报错
class Person(object):
def __int__(self,name,gender,birth):
self.name=name
self.gender=gender
self.birth=birth
p1=Person('Xiao Ming','Male','1991-1-1')
p2=Person('Xiao Hong','Female','1992-2-2')
print p1.name
为什么会报错
2018-10-15
因为题目说:还可接受任意关键字参数,并把他们都作为属性赋值给实例。你漏了这个。
for k,v in kw.iteritems():
setattr(self,k,v)
需要加上这句!
总的:
class Person(object):
def __init__(self,name,gender,birth,**kw):
self.name='Xiao Ming'
self.name='Male'
self.name='1990-1-1'
for k,v in kw.iteritems():
setattr(self,k,v)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
举报