現在『楽しいプログラミング pythonで始めよう』という本でpythonを勉強中です。その中で、classを使ったコードの例として、
python3
1class Animal: 2 def __int__(self,species,number_of_legs,color): 3 self.species=species 4 self.number_of_legs=number_of_legs 5 self.color=color 6 7harry=Animal("hippogriff",6,"pink") 8print(harry.species) 9
という例文がありました。しかし、この文をpython3.5のidle.exeで実行したところ、
TypeError: object() takes no parameters
とエラーが出ました。そこで、次のようにコードを書きなおしました。
python3
1 2import copy 3class Animal: 4 def __int__(self,species,number_of_legs,color): 5 self.species=species 6 self.number_of_legs=number_of_legs 7 self.color=color 8 9harry=Animal() 10harry.__int__("hippogriff",6,"pink") 11print(harry.species)
今度はエラーが起きませんでした。他のページでも同様のコードの書き方だったため、本が間違っているということはないと思います。最初のコードではなぜプログラムが動作しなかったのでしょうか。また、修正の方法としてもっとよい書き方はありますでしょうか。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/12 15:30