必须使用 fibo_ 实例作为第一个参数调用未绑定方法 f() (改为使用 classobj 实例)

发布于 2024-10-08 07:24:39 字数 627 浏览 4 评论 0原文

在 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)

Code: (swineflu.py)

class fibo:
    a=0
    b=0

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

Script main.py

import swineflu

f = swineflu
fibo = f.fibo

fibo.f()            #TypeError is thrown here

此错误意味着什么?是什么导致了这个错误?

In Python, I'm trying to run a method in a class and I get an error:

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)

Code: (swineflu.py)

class fibo:
    a=0
    b=0

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

Script main.py

import swineflu

f = swineflu
fibo = f.fibo

fibo.f()            #TypeError is thrown here

What does this error mean? What is causing this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

↘人皮目录ツ 2024-10-15 07:24:40

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

>>> 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. 类和对象之间的差异。

How to reproduce this error with as few lines as possible:

>>> 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)

It fails because of TypeError because you didn't instantiate the class first, you have two choices: 1: either make the method static so you can run it in a static way, or 2: instantiate your class so you have an instance to grab onto, to run the method.

It looks like you want to run the method in a static way, do this:

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

Or, what you probably meant is to use the instantiated instance like this:

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

If this confuses you, ask these questions:

  1. What is the difference between the behavior of a static method vs the behavior of a normal method?
  2. What does it mean to instantiate a class?
  3. Differences between how static methods are run vs normal methods.
  4. Differences between class and object.
执着的年纪 2024-10-15 07:24:40

fibo = f.fibo 引用类本身。您可能想要 fibo = f.fibo() (注意括号)创建该类的实例,然后创建 fibo.f() 应该正确成功。

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

fibo = f.fibo references the class itself. You probably wanted fibo = f.fibo() (note the parentheses) to make an instance of the class, after which fibo.f() should succeed correctly.

f.fibo.f() fails because you are essentially calling f(self, a=0) without supplying self; self is "bound" automatically when you have an instance of the class.

山色无中 2024-10-15 07:24:40

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

如果您

a = fibo()
a.f()

这样做了,那么 f 就会被绑定(到实例 a)。

f is an (instance) method. However, you are calling it via fibo.f, where fibo is the class object. Hence, f is unbound (not bound to any class instance).

If you did

a = fibo()
a.f()

then that f is bound (to the instance a).

玩套路吗 2024-10-15 07:24:40

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

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

使用 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')

In Python 2 (3 has different syntax):

What if you can't instantiate your Parent class before you need to call one of its methods?

Use super(ChildClass, self).method() to access parent methods.

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')
日久见人心 2024-10-15 07:24:40
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`. 

这里是一个很好的教程,用于开始使用 Python 中的类。

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 is a good tutorial to get started with classes in Python.

只怪假的太真实 2024-10-15 07:24:40

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
我想这可能对你有帮助。

Differences in In python 2 and 3 version:

If you already have a default method in a class with same name and you re-declare as a same name it will appear as unbound-method call of that class instance when you wanted to instantiated it.

If you wanted class methods, but you declared them as instance methods instead.

An instance method is a method that is used when to create an instance of the class.

An example would be

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

Class label method:

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

In python 2 and 3 version differ the class @classmethod to write
in python 3 it automatically get that as a class-label method and don't need to write @classmethod
I think this might help you.

蓝梦月影 2024-10-15 07:24:40

试试这个。对于 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)

Try this. For python 2.7.12 we need to define constructor or need to add self to each methods followed by defining an instance of an class called 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)
追星践月 2024-10-15 07:24:39

好的,首先,您不必将对模块的引用更改为不同的名称;您已经有一个参考(来自 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

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

OK, first of all, you don't have to get a reference to the module into a different name; you already have a reference (from the import) and you can just use it. If you want a different name just use import swineflu as f.

Second, you are getting a reference to the class rather than instantiating the class.

So this should be:

import swineflu

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

A bound method is one that is attached to an instance of an object. An unbound method is, of course, one that is not attached to an instance. The error usually means you are calling the method on the class rather than on an instance, which is exactly what was happening in this case because you hadn't instantiated the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文