Error when trying to use _init_ in Python 3.7.0 IDLE

我的梦境 提交于 2021-01-29 10:27:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!