Python 'takes exactly 1 argument (2 given)' Python error
生活随笔
收集整理的這篇文章主要介紹了
Python 'takes exactly 1 argument (2 given)' Python error
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python初學,定義class的函數之后,在調用的時候出現“'takes exactly 1 argument (2 given)' Python error”。
查詢Interesting 'takes exactly 1 argument (2 given)' Python error?之后,原來在python中,在使用instance調用其class的方法的時候,是相當與在調用中加入本身作為第一個參數的。如下:
a.method(k)
就相當與:
a.method(a, k)
所以,在對函數進行定義的時候,需要在函數內部參數中第一個定義為self,如:
class Person():
? ? def method(self, k):
? ? ? ? ...
如果想作為靜態函數調用,可以在定義函數上方加上“@staticmethod”來進行標識。
總結
以上是生活随笔為你收集整理的Python 'takes exactly 1 argument (2 given)' Python error的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习中的lazy method与ea
- 下一篇: 使用KNN对MNIST数据集进行实验