问题:未绑定方法f()必须以fibo_实例作为第一个参数调用(取而代之的是class classobj实例)
在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
这个错误是什么意思?是什么导致此错误?
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?
回答 0
好的,首先,您不必以其他名称引用模块。您已经有一个参考(来自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.
回答 1
如何用尽可能少的行来重现此错误:
>>> 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
如果这使您感到困惑,请提出以下问题:
- 静态方法的行为与普通方法的行为有什么区别?
- 实例化类是什么意思?
- 静态方法的运行方式与普通方法之间的差异。
- 类和对象之间的差异。
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:
- 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.
回答 2
fibo = f.fibo
引用类本身。您可能希望fibo = f.fibo()
(请注意括号)成为该类的实例,之后fibo.f()
应该可以成功完成。
f.fibo.f()
之所以失败,是因为您本质上f(self, a=0)
没有打电话就打电话self
; 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.
回答 3
f
是(实例)方法。但是,您是通过调用它的fibo.f
,那里fibo
是类对象。因此,f
是unbound(不绑定到任何类实例)。
如果你做了
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
).
回答 4
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.
回答 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')
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')
回答 6
在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版本中,要在python 3中编写的类@classmethod有所不同,它会自动将其作为类标签方法获取,而无需编写@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.
回答 7
试试这个。对于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)