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?
这个错误是什么意思?是什么导致了这个错误?
7 个解决方案
#1
161
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
.
好的,首先,你不需要将模块引用到一个不同的名称;您已经有了一个引用(来自导入),您可以使用它。如果你想要一个不同的名字,就用进口猪流感作为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.
绑定方法是附加到对象实例的方法。当然,一个未绑定的方法是不附加在实例上的。错误通常意味着在类上调用方法而不是在实例上调用,这正是在本例中发生的事情,因为您没有实例化该类。
#2
62
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.
它失败是因为类型错误,因为您没有首先实例化类,您有两个选择:1:要么使方法静态,以便您可以以静态方式运行它,或者2:实例化您的类,这样您就有一个实例来获取,来运行这个方法。
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.
- 类和对象之间的区别。
#3
9
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.
调用fibo = f。fibo引用类本身。您可能希望fibo = f.fibo()(注意圆括号)来创建类的实例,然后fibo.f()应该能够正确地获得成功。
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.
f()失败,因为你本质上是调用f(self, a=0)而不提供自我;当你有一个类的实例时,self会自动“绑定”。
#4
3
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).
f是一个(实例)方法。但是,你是通过fibo来调用的。fibo是类对象。因此,f是未绑定的(不绑定到任何类实例)。
If you did
如果你做
a = fibo()
a.f()
then that f
is bound (to the instance a
).
然后f被绑定(到实例a)。
#5
2
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.
这里有一个很好的教程,可以开始使用Python中的类。
#6
2
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?
如果在需要调用它的方法之前,不能实例化您的父类,该怎么办?
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')
#7
0
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.
如果您已经在一个同名的类中有一个默认的方法,并且您重新声明为一个相同的名称,那么当您想要实例化它时,它将作为类实例的unbound方法调用出现。
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,我想这可能会对您有所帮助。
#1
161
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
.
好的,首先,你不需要将模块引用到一个不同的名称;您已经有了一个引用(来自导入),您可以使用它。如果你想要一个不同的名字,就用进口猪流感作为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.
绑定方法是附加到对象实例的方法。当然,一个未绑定的方法是不附加在实例上的。错误通常意味着在类上调用方法而不是在实例上调用,这正是在本例中发生的事情,因为您没有实例化该类。
#2
62
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.
它失败是因为类型错误,因为您没有首先实例化类,您有两个选择:1:要么使方法静态,以便您可以以静态方式运行它,或者2:实例化您的类,这样您就有一个实例来获取,来运行这个方法。
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.
- 类和对象之间的区别。
#3
9
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.
调用fibo = f。fibo引用类本身。您可能希望fibo = f.fibo()(注意圆括号)来创建类的实例,然后fibo.f()应该能够正确地获得成功。
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.
f()失败,因为你本质上是调用f(self, a=0)而不提供自我;当你有一个类的实例时,self会自动“绑定”。
#4
3
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).
f是一个(实例)方法。但是,你是通过fibo来调用的。fibo是类对象。因此,f是未绑定的(不绑定到任何类实例)。
If you did
如果你做
a = fibo()
a.f()
then that f
is bound (to the instance a
).
然后f被绑定(到实例a)。
#5
2
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.
这里有一个很好的教程,可以开始使用Python中的类。
#6
2
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?
如果在需要调用它的方法之前,不能实例化您的父类,该怎么办?
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')
#7
0
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.
如果您已经在一个同名的类中有一个默认的方法,并且您重新声明为一个相同的名称,那么当您想要实例化它时,它将作为类实例的unbound方法调用出现。
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,我想这可能会对您有所帮助。