Pythonのクラスをインスタンス化した時、不思議な動きをします。何か意味がありますか?
Pyhton
1class A: 2 i = 12345 3 def f(self): 4 print("hello world")
通常
Pyhton
1a = A() 2print(a.i) 3print(a.f())
12345
hello world
特殊なインスタンス化
Pyhton
1a = A 2print(a.i) 3print(a.f())
12345
TypeError Traceback (most recent call last)
<ipython-input-28-4d64934cde54> in <module>()
6 a = A
7 print(a.i)
----> 8 print(a.f())TypeError: f() missing 1 required positional argument: 'self'
Pyhton
1a = A 2print(a.i) 3print(a.f("foo"))
12345
hello world
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/31 13:01