没事想学
2016-10-27 21:45
写了下老师课堂上的代码,运行起来老是报错,发现只要带了参数就报错。这是为什么?
class Programer(object):
def __new__(cls, *args, **kwargs):
print ("new", cls)
print(args)
return super(Programer,cls).__new__(cls, *args, **kwargs)
def __init__(self,name = None,age= None ,language= None):
self.name = name
self.age = age
self.language = language
if __name__ == '__main__':
programer_unerror = Programer()
print(programer_unerror.__dict__)
programer_error = Programer('Alex')
print(programer_error.__dict__)
我在python2.7上正常,python3.5出现了以上的问题。
如何修复:
return super(Programer,cls).__new__(cls, *args, **kwargs)
修改为:
return super(Programer,cls).__new__(cls)
这里讲的很清楚:
http://stackoverflow.com/questions/34777773/typeerror-object-takes-no-parameters-after-defining-new
Python-面向对象
71226 学习 · 83 问题
相似问题