此_init_ [关闭]有什么问题

问题描述 投票:0回答:1
此python代码有什么问题?我一直在尝试学习如何使用_init_,但无法使其正常工作

class Giraffes: def _init_(self, spots): self.giraffe_spots = spots ozwald = Giraffes(100) print(ozwald.giraffe_spots)

python python-3.x
1个回答
1
投票
def __init__(self, spots):

您只在任一侧使用了一个。如果拼写错误,则在创建新实例时将不会调用它。

演示:
>>> class Giraffes:
...     def __init__(self, spots):
...         self.giraffe_spots = spots
... 
>>> ozwald = Giraffes(100)
>>> print(ozwald.giraffe_spots)
100
© www.soinside.com 2019 - 2024. All rights reserved.