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.
Python では、メソッドでパラメーターを受け入れるクラスを定義する場合__init__:
__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クラスメソッドの最初の引数として配置する必要があります。