Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
__init__在 Python 中,当定义一个在其方法中接受参数的类时:
__init__
class animal: number_of_legs = 0 def __init__(nlegs, self): self.number_of_legs = nlegs a = animal(3)
我收到以下错误:
AttributeError:“int”对象没有属性“number_of_legs”
改变:
def __init__(nlegs, self):
至:
def __init__(self, nlegs):
因为在您的代码实例中被分配给nlegs并且 3 被分配给self.
nlegs
self
您应该将self其作为类方法中的第一个参数。