必须使用 fibo_ 实例作为第一个参数调用未绑定方法 f() (改为使用 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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如何用尽可能少的行重现此错误:
由于 TypeError 而失败,因为您没有首先实例化该类,您有两个选择: 1:要么使方法静态,以便可以以静态方式运行它,要么2:实例化您的类,以便您有一个可以抓住的实例来运行该方法。
看起来您想以静态方式运行该方法,请执行以下操作:
或者,您可能的意思是使用如下所示的实例化实例:
如果这让您感到困惑,请提出以下问题:
How to reproduce this error with as few lines as possible:
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:
Or, what you probably meant is to use the instantiated instance like this:
If this confuses you, ask these questions:
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 wantedfibo = f.fibo()
(note the parentheses) to make an instance of the class, after whichfibo.f()
should succeed correctly.f.fibo.f()
fails because you are essentially callingf(self, a=0)
without supplyingself
;self
is "bound" automatically when you have an instance of the class.f
是一个(实例)方法。但是,您是通过 fibo.f 调用它的,其中 fibo 是类对象。因此,f
是未绑定的(未绑定到任何类实例)。如果您
这样做了,那么
f
就会被绑定(到实例a
)。f
is an (instance) method. However, you are calling it viafibo.f
, wherefibo
is the class object. Hence,f
is unbound (not bound to any class instance).If you did
then that
f
is bound (to the instancea
).在 Python 2 中(3 有不同的语法):
如果在需要调用父类的方法之一之前无法实例化父类怎么办?
使用 super(ChildClass, self).method() 访问父方法。
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.这里是一个很好的教程,用于开始使用 Python 中的类。
Here is a good tutorial to get started with classes in Python.
python 2 和 3 版本中的差异:
如果您在同名的类中已经有一个默认方法,并且您重新声明为同名,那么当您想要实例化它时,它将显示为该类实例的未绑定方法调用。
如果您想要类方法,但您将它们声明为实例方法。
实例方法是创建类的实例时使用的方法。
一个例子是
类标签方法:
在 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
Class label method:
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.7.12,我们需要定义构造函数,或者需要将 self 添加到每个方法,然后定义一个名为 object 的类的实例。
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
),您可以直接使用它。如果您想要不同的名称,只需使用import swineflu as f
。其次,您将获得对该类的引用,而不是实例化该类。
所以这应该是:
绑定方法是附加到对象实例的方法。当然,未绑定方法是指未附加到实例的方法。该错误通常意味着您正在类上而不是实例上调用该方法,这正是本例中发生的情况,因为您尚未实例化该类。
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 useimport swineflu as f
.Second, you are getting a reference to the class rather than instantiating the class.
So this should be:
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.