什么是“可调用”?现在清楚了什么是元类,有一个相关的概念,我一直在使用,不知道它的真正含义。我想每个人都犯过一次括号错误,导致了“Object不是可调用的”异常。更重要的是,使用__init__和__new__让人好奇这该死的是什么__call__可以用来。你能给我一些解释吗,包括魔术法的例子?
3 回答

慕斯王
TA贡献1864条经验 获得超2个赞
类的实例。 _呼叫_
方法或 是具有非空值的类型。 TP调用
(C Struct)以其他方式表示可调用性的成员(如函数、方法等)
当实例作为函数被“调用”时调用
例
class Foo: def __call__(self): print 'called'foo_instance = Foo()foo_instance() #this is calling the __call__ method
添加回答
举报
0/150
提交
取消