您的位置:首页 > 编程语言 > Go语言

TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

2015-03-28 11:40 513 查看
错误如下:

>>> class A:
def a(self):
print("I'm a")

>>> A.a()

Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
A.a()
TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

错误原因:函数a()非静态方法,故需实例化然后才能使用,改正如下:
class A:
def a(self):
print("I'm a")

obj = A()
obj.a()

或者将a()改为静态方法即可,如下:
[code]class A:
@staticmethod
def a():
print ("I'm a")

A.a()

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐