Python 'takes exactly 1 argument (2 given)' Python error
2014-03-14 21:28
78 查看
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”来进行标识。
查询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: TypeError: XXX() takes exactly 1 argument (2 given)
- Python 'takes exactly 1 argument (2 given)' Python error
- python:PyInstaller打包发布单个文件错误struct.error: argument for 's' must be a string解决
- python32版本,出现问题“struct.error: argument for 's' must be a bytes object”的解决办法
- Python 'takes exactly 1 argument (2 given)' Python error
- python常用模块介绍(转自itech's blog)
- python中if __name__ == '__main__': 的解析
- clang: error: unknown argument: '-mno-fused-madd
- [python]Django AttributeError 'tuple' object has no attribute 'regex'
- python中if __name__ == '__main__': 的解析
- Python ''.JOIN()的作用
- Python's Hello,world
- Python脚本语法错误:SyntaxError:(unicode error) 'utf8' codec can't decode byte 0xc0 in position 0: invalids
- Understanding Python's "with" statement
- python mysqldb 数据库插入中文 “"UnicodeEncodeError:'latin-1' codec can't encode character ..."”
- 在 Python中使用 cout << 'Hello,world!' << endl
- python遇到AttributeError: 'str' object has no attribute '_getAssertEqualityFunc'问题解决过程
- Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法
- Python 安装Setup.py (error package directory 'src atom' does not exist)
- python中if __name__ == '__main__'的作用