当前位置: 首页 > 工具软件 > Unbound > 使用案例 >

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

爱炯
2023-12-01

错误如下:

>>> 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()改为静态方法即可,如下:
class A:
    @staticmethod
    def a():
        print ("I'm a")

A.a()




 类似资料:

相关阅读

相关文章

相关问答