【问题标题】:unbound method f() must be called with fibo_ instance as first argument (got classobj instance instead)必须使用 fibo_ 实例作为第一个参数调用未绑定的方法 f()(改为获取 classobj 实例)
【发布时间】:2011-05-27 06:27:26
【问题描述】:

在 Python 中,我试图在类中运行一个方法,但出现错误:

Traceback (most recent call last):
  File "C:\Users\domenico\Desktop\py\main.py", line 8, in <module>
    fibo.f()
  TypeError: unbound method f() must be called with fibo instance 
  as first argument (got nothing instead)

代码:(swineflu.py)

class fibo:
    a=0
    b=0

    def f(self,a=0):
        print fibo.b+a
        b=a;
        return self(a+1)

脚本 main.py

import swineflu

f = swineflu
fibo = f.fibo

fibo.f()            #TypeError is thrown here

这个错误是什么意思?是什么导致了这个错误?

【问题讨论】:

  • 是否要实例化一个对象?
  • 类名应大写。
  • fibo = f.fibo() 需要用括号来实例化类。
  • 你可以使用fibo().f()

标签: python methods


【解决方案1】:

好的,首先,您不必将模块的引用换成不同的名称;你已经有一个参考(来自import),你可以使用它。如果您想要不同的名称,只需使用import swineflu as f

其次,你得到的是对类的引用,而不是实例化类。

所以应该是这样的:

import swineflu

fibo = swineflu.fibo()  # get an instance of the class
fibo.f()                # call the method f of the instance

绑定方法是一种附加到对象实例的方法。当然,未绑定方法附加到实例的方法。该错误通常意味着您在类而不是实例上调用方法,这正是在这种情况下发生的情况,因为您尚未实例化该类。

【讨论】:

  • 如果您只调用一次,也可以使用swineflu.fibo().f()
【解决方案2】:

如何用尽可能少的行重现此错误:

>>> class C:
...   def f(self):
...     print "hi"
...
>>> C.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method f() must be called with C instance as 
first argument (got nothing instead)

由于 TypeError 失败,因为您没有先实例化该类,您有两个选择:1:将方法设为静态,以便您可以以静态方式运行它,或 2:实例化您的类,以便您拥有要抓取的实例,以运行该方法。

您似乎想以静态方式运行该方法,请执行以下操作:

>>> class C:
...   @staticmethod
...   def f():
...     print "hi"
...
>>> C.f()
hi

或者,您可能的意思是像这样使用实例化的实例:

>>> class C:
...   def f(self):
...     print "hi"
...
>>> c1 = C()
>>> c1.f()
hi
>>> C().f()
hi

如果这让您感到困惑,请提出以下问题:

  1. 静态方法的行为与普通方法的行为有什么区别?
  2. 实例化一个类是什么意思?
  3. 静态方法与普通方法的运行方式之间的差异。
  4. 类和对象之间的区别。

【讨论】:

  • 我已经实例化了我的类,但它只在我使用@staticmethod 时才有效。可以解释一下吗?
【解决方案3】:

fibo = f.fibo 引用类本身。您可能希望 fibo = f.fibo()(注意括号)创建类的实例,之后fibo.f() 应该会成功。

f.fibo.f() 失败,因为您实际上是在调用f(self, a=0) 而不提供self;当您拥有该类的实例时,self 会自动“绑定”。

【讨论】:

    【解决方案4】:

    f 是一个(实例)方法。但是,您通过fibo.f 调用它,其中fibo 是类对象。因此,f 是未绑定的(未绑定到任何类实例)。

    如果你这样做了

    a = fibo()
    a.f()
    

    那么f 被绑定(到实例a)。

    【讨论】:

      【解决方案5】:

      在 Python 2 中(3 有不同的语法):

      如果您无法在需要调用 Parent 类的方法之一之前实例化它怎么办?

      使用super(ChildClass, self).method() 访问父方法。

      class ParentClass(object):
          def method_to_call(self, arg_1):
              print arg_1
      
      class ChildClass(ParentClass):
          def do_thing(self):
              super(ChildClass, self).method_to_call('my arg')
      

      【讨论】:

        【解决方案6】:
        import swineflu
        
        x = swineflu.fibo()   # create an object `x` of class `fibo`, an instance of the class
        x.f()                 # call the method `f()`, bound to `x`. 
        

        Here 是一个很好的 Python 类入门教程。

        【讨论】:

          【解决方案7】:

          python 2 和 3 版本的区别:

          如果您在同名的类中已经有一个默认方法,并且您重新声明为同名,那么当您想要实例化它时,它将显示为该类实例的未绑定方法调用。

          如果您想要类方法,但您将它们声明为实例方法。

          实例方法是在创建类的实例时使用的方法。

          一个例子是

             def user_group(self):   #This is an instance method
                  return "instance method returning group"
          

          类标签方法:

             @classmethod
             def user_group(groups):   #This is an class-label method
                  return "class method returning group"
          

          在 python 2 和 3 版本不同的类@classmethod 来写 在 python 3 中,它会自动将其作为类标签方法获取,并且不需要编写 @classmethod 我想这可能会对你有所帮助。

          【讨论】:

            【解决方案8】:

            试试这个。对于 python 2.7.12,我们需要定义构造函数或需要将 self 添加到每个方法,然后定义一个名为 object 的类的实例。

            import cv2
            
            class calculator:
            
            #   def __init__(self):
            
            def multiply(self, a, b):
                x= a*b
                print(x)
            
            def subtract(self, a,b):
                x = a-b
                print(x)
            
            def add(self, a,b):
                x = a+b
                print(x)
            
            def div(self, a,b):
                x = a/b
                print(x)
            
             calc = calculator()
             calc.multiply(2,3)
             calc.add(2,3)
             calc.div(10,5)
             calc.subtract(2,3)
            

            【讨论】:

              猜你喜欢
              • 2018-05-10
              • 2018-02-18
              • 2023-04-06
              • 1970-01-01
              • 2017-12-09
              • 2015-12-01
              • 2018-08-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多