翻译:
类型错误: object()不接受参数
案例:(错误代码)
class Student(object):
def __int__(self, name, score):
self.name = name
self.score = score
def print_score(self):
print self.score
Ronny = Student('Ronny', 97)
Ronny.print_score()
Ronny.age = 22
print Ronny.age
报错:
F:\pythonTest>python TestClass.py
Traceback (most recent call last):
File "TestClass.py", line 10, in <module>
Ronny = Student('Ronny', 97)
Type Error: object() takes no parameters
翻译:
F:\ pythonTest python TestClass.py
回溯(最近一次通话):
TestClass文件”。第10行,<模块>。
罗尼=学生(罗尼,97)
类型错误:object()不接受参数
分析:
在构建对象时报错,类不需要参数,而我已经定义了默认构造函数是需要两个参数的,这是为什么的?原因大概率在定义的构造函数上,应该是没有调用到它。
检查一番,果然发现,默认构造函数是__init__,而我写成了__int__,我用的是UltraEdit,一样会提示颜色变换,
所以没有注意到,太粗心了!
更改后如下:
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def print_score(self):
print self.score
Ronny = Student('Ronny', 97)
Ronny.print_score()
Ronny.age = 22
print Ronny.age
测试:
成功运行
同时,发现python中构造完成的对象可以随意增加属性变量,如图中Ronny这个学生对象,可以增加类定义中没有的age属性。