Python 必须使用 fibo_ 实例作为第一个参数调用未绑定的方法 f()(改为使用 classobj 实例)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4473184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unbound method f() must be called with fibo_ instance as first argument (got classobj instance instead)
提问by DomeWTF
In Python, I'm trying to run a method in a class and I get an error:
在 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)
代码:(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
脚本 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?
这个错误是什么意思?是什么导致了这个错误?
采纳答案by kindall
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.
好的,首先,您不必将对该模块的引用转换为不同的名称;您已经有一个参考(来自import),您可以直接使用它。如果您想要不同的名称,请使用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 methodis one that is attached to an instance of an object. An unbound methodis, of course, one that is notattached 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.
甲绑定的方法是一个被附加到对象的实例。的未结合的方法是,当然,一个是未附连到一个实例。该错误通常意味着您是在类上而不是在实例上调用该方法,这正是本例中发生的情况,因为您尚未实例化该类。
回答by Mark Rushakoff
fibo = f.fiboreferences the class itself. You probably wanted fibo = f.fibo()(note the parentheses) to make an instanceof the class, after which fibo.f()should succeed correctly.
fibo = f.fibo引用类本身。您可能想要fibo = f.fibo()(注意括号)创建类的实例,之后fibo.f()应该会正确成功。
f.fibo.f()fails because you are essentially calling f(self, a=0)without supplying self; selfis "bound" automatically when you have an instance of the class.
f.fibo.f()失败,因为您本质上是在f(self, a=0)不提供的情况下调用self;self当你有一个类的实例时,它会自动“绑定”。
回答by lijie
fis an (instance) method. However, you are calling it via fibo.f, where fibois the class object. Hence, fis unbound (not bound to any class instance).
f是一个(实例)方法。但是,您通过 调用它fibo.f,fibo类对象在哪里。因此,f是未绑定的(未绑定到任何类实例)。
If you did
如果你做了
a = fibo()
a.f()
then that fis bound (to the instance a).
然后f绑定(到实例a)。
回答by user225312
回答by Eric Leschinski
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.
它因为 TypeError 而失败,因为您没有先实例化类,您有两种选择:1:使方法静态以便您可以静态方式运行它,或者 2:实例化您的类,以便您有一个实例可以抓取on,运行该方法。
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:
如果这让您感到困惑,请提出以下问题:
- What is the difference between the behavior of a static method vs the behavior of a normal method?
- What does it mean to instantiate a class?
- Differences between how static methods are run vs normal methods.
- Differences between class and object.
- 静态方法的行为与普通方法的行为有什么区别?
- 实例化一个类是什么意思?
- 静态方法与普通方法的运行方式之间的差异。
- 类和对象的区别。
回答by Peter Graham
In Python 2 (3 has different syntax):
在 Python 2 中(3 有不同的语法):
What if you can't instantiate your Parent class before you need to call one of its methods?
如果在需要调用其方法之一之前无法实例化 Parent 类怎么办?
Use super(ChildClass, self).method()to access parent methods.
使用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')
回答by Projesh Bhoumik
Differences in In python 2 and 3 version:
在 python 2 和 3 版本中的差异:
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.
在python 2和3版本中,在python 3中编写的类@classmethod不同,它会自动将其作为类标签方法而不需要编写@classmethod,我认为这可能对您有所帮助。
回答by NIKHIL BHALODIYA
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.
尝试这个。对于 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)

