Python 类型错误:“类”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19752634/
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
TypeError: 'class' object is not callable
提问by Black_Ram
I have got:
我有:
MAIN SCRIPT:
主脚本:
import music.umm
UMM = music.umm.UMM()
UMM.read_information()
MODULE SCRIPT:
模块脚本:
class UMM(object):
def read_information(self):
..some code
UMM.login()
UMM = UMM()
With this code, I get this error when I run main script:
使用此代码,运行主脚本时出现此错误:
TypeError: 'UMM' object is not callable
类型错误:“UMM”对象不可调用
How do I fix it?
我如何解决它?
EDIT:
编辑:
I remove from the module this line:
我从模块中删除了这一行:
UMM = UMM()
Now, the main script RUN the function module, but if the script module goes to another function, I get this error:
现在,主脚本运行功能模块,但如果脚本模块转到另一个功能,我会收到此错误:
TypeError: unbound method login() must be called with UMM instance as first argument (got nothing instead)
类型错误:必须使用 UMM 实例作为第一个参数调用未绑定的方法 login()(什么都没有)
采纳答案by glglgl
I am trying to help in spite of the little information you give us.
尽管您给我们提供的信息很少,但我还是会尽力提供帮助。
An SSCCE could look like
SSCCE 可能看起来像
umm.py:
嗯.py:
class UMM(object):
def login(self):
print("login()")
def read_information(self):
print("read_info() 1")
UMM.login()
print("read_info() 2")
main script:
主要脚本:
import umm
umm = umm.UMM()
umm.read_information()
I didn't test it, but I imagine that this would yield exactly the following exception
我没有测试它,但我想这会产生以下异常
TypeError: unbound method login() must be called with UMM instance as first argument (got nothing instead)
The reason is that UMM.login()
is a method which expects to be called via an instance of the object.
原因是这UMM.login()
是一个期望通过对象实例调用的方法。
Inside read_information()
, you have self
as a concrete object instance. So you could replace the call
在里面read_information()
,你有self
一个具体的对象实例。所以你可以更换电话
UMM.login()
with
和
self.login()
in order to fulfill all dependencies.
为了满足所有的依赖。
A call to UMM.login()
would try to call login()
without a object instance to work on. This would work with a @staticmethod
or a @classmethod
, but not with a regular bound method.
调用UMM.login()
将尝试在login()
没有对象实例的情况下调用。这适用于 a@staticmethod
或 a @classmethod
,但不适用于常规绑定方法。
回答by Ewan
class
is a reserved keyword for defining a class.
class
是用于定义类的保留关键字。
Try naming your class something different:
尝试为您的班级命名不同的名称:
class YourClass(object):
..functions..
Then initialise it with:
然后初始化它:
var_class = YourClass()
回答by AdamD
This is how I do it:
这就是我的做法:
# Module Code
class MyClass(object):
def foo(self):
print "Foo"
# Client Code
from MyClass import MyClass
inst = MyClass()
inst.foo()
回答by lstodd
You need to initiate an instance of the class.
您需要启动该类的一个实例。
You attempted to do that with:
你试图这样做:
UMM = UMM()
You can initiate an instance of the class by doing something like:
您可以通过执行以下操作来启动类的实例:
umm = UMM()
i.e. not overwriting the class name.
即不覆盖类名。