问题
So I was just trying to do same basic Classes stuff in Python.org's Python 3.7.0 IDLE (Shell), and when I tried to run this code:
class Giraffes:
def _init_(self, spots):
self.giraffe_spots = spots
gerald = Giraffes(100)
It gave me this error:
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
gerald = Giraffes(100)
TypeError: Giraffes() takes no arguments
Doesn't the init function in class Giraffes make Gerald take parameter self?
I'm just getting started with Python so sorry if this is a newbie mistake or something.
回答1:
The constructor method (like other Python magic methods) is called __init__
(two underscores on each side).
来源:https://stackoverflow.com/questions/51163528/error-when-trying-to-use-init-in-python-3-7-0-idle