第一个错误
正确的代码如下:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f = FooBar('wrong')
TypeError: this constructor takes no arguments
源码如下:
class FooBar:
def _init_(self, value):
self.somevar = value错误原因:
构造函数,是两个下划线,不是一个!
正确的代码如下:
class FooBar: def __init__(self, value): self.somevar = value
本文介绍了一个常见的Python类构造函数定义错误,并提供了正确的解决方案。错误来源于使用了错误的构造函数名称,正确的构造函数应该包含双下划线而非单下划线。
3万+

被折叠的 条评论
为什么被折叠?



