Pythonで
クラスを作ってメソッドを作ったらエラーになった。
takes 0 positional arguments but 1 was given
https://qiita.com/matsuida55/items/4188430631353a7a1194
「pythonはクラスメソッドの引数1つ目に必ず self を取らなければならない。」
・結論
クラスのメソッドの引数は必要なくても、 引数selfを入れなくてはならない。
誤り:エラーになるtakes 0 positional arguments but 1 was given
#python class サンプル class nikkeiClass: def __init__(self, cindex, chigh): self.index = cindex self.high = chigh def sample():#引数はないのでブランクにした>> ここが誤り print(self.index) ##START test=nikkeiClass("aa","bb") test.sample() >>>エラー発生 takes 0 positional arguments but 1 was given
↓
正しい
#python class サンプル class nikkeiClass: def __init__(self, cindex, chigh): self.index = cindex self.high = chigh def sample(self):#引数にselfがないとエラーになる print(self.index) ##START test=nikkeiClass("aa","bb") test.sample() 出力結果 aa