为什么在尝试使用 _init_ 时会出现错误?
Why do I get an error when trying to use _init_?
所以我只是想在我的 Python 3.7.0 IDLE (Shell) 中做来自 python.org 的相同基本 classes 示例,当我尝试 运行 此代码:
class Giraffes:
def _init_(self, spots):
self.giraffe_spots = spots
gerald = Giraffes(100)
它给了我这个错误:
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
gerald = Giraffes(100)
TypeError: Giraffes() takes no arguments
classGiraffes
中的init函数不是让Gerald带参数self
吗?
constructor method (like other Python magic methods) 称为__init__
(每边两个下划线)。
所以我只是想在我的 Python 3.7.0 IDLE (Shell) 中做来自 python.org 的相同基本 classes 示例,当我尝试 运行 此代码:
class Giraffes:
def _init_(self, spots):
self.giraffe_spots = spots
gerald = Giraffes(100)
它给了我这个错误:
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
gerald = Giraffes(100)
TypeError: Giraffes() takes no arguments
classGiraffes
中的init函数不是让Gerald带参数self
吗?
constructor method (like other Python magic methods) 称为__init__
(每边两个下划线)。