class Class:
def method():
print('I have a self!')
>>> instance=Class()
>>> instance.method()
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
instance.method()
TypeError: method() takes 0 positional arguments but 1 was given
請問一下,在定義類的時候,方法method中未定義為method(self),為什么會報錯?
我查閱了相關的知識點,instance是類Class的實體,instance.method()是使用instance呼叫類中的方法,相當于Class.method(),但是方法中的代碼未涉及到self相關的屬性,例如self.name,為什么還需要將方法寫成method(self),我不是很理解,謝謝大佬給予解答!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283571.html
上一篇:tensorflow中的DataFormatVecPermute()算子如何使用
下一篇:求大佬幫忙看一下這道題
