现在,元类是什么已经很清楚了,有一个相关的概念,我一直在使用,但不知道它的真正含义。
我想每个人都犯过一次圆括号错误,导致“对象不可调用”异常。更重要的是,使用__init__和__new__会导致想知道这个该死的__call__可以用来做什么。
你能给我一些解释吗,包括魔术方法的例子?
现在,元类是什么已经很清楚了,有一个相关的概念,我一直在使用,但不知道它的真正含义。
我想每个人都犯过一次圆括号错误,导致“对象不可调用”异常。更重要的是,使用__init__和__new__会导致想知道这个该死的__call__可以用来做什么。
你能给我一些解释吗,包括魔术方法的例子?
当前回答
检查类的函数或方法是否可调用,这意味着我们可以调用该函数。
Class A:
def __init__(self,val):
self.val = val
def bar(self):
print "bar"
obj = A()
callable(obj.bar)
True
callable(obj.__init___)
False
def foo(): return "s"
callable(foo)
True
callable(foo())
False
其他回答
在Python中,可调用对象是一个类型具有__call__方法的对象:
>>> class Foo:
... pass
...
>>> class Bar(object):
... pass
...
>>> type(Foo).__call__(Foo)
<__main__.Foo instance at 0x711440>
>>> type(Bar).__call__(Bar)
<__main__.Bar object at 0x712110>
>>> def foo(bar):
... return bar
...
>>> type(foo).__call__(foo, 42)
42
就这么简单:)
这当然可以重载:
>>> class Foo(object):
... def __call__(self):
... return 42
...
>>> f = Foo()
>>> f()
42
你可以在它后面加上“(args)”,并期望它能工作。可调用对象通常是一个方法或类。方法被调用,类被实例化。
很简单,“可调用对象”是可以像方法一样调用的对象。内置函数“callable()”将告诉您某些东西是否看起来是可调用的,就像检查call属性一样。函数和类一样都是可调用的,类实例也是可调用的。看这里和这里。
具有__call__()的类、函数、方法和对象都是可调用的。
你可以用callable()检查是否可调用,如果可调用返回True,如果不可调用返回False,如下所示:
class Class1:
def __call__(self):
print("__call__")
class Class2:
pass
def func():
pass
print(callable(Class1)) # Class1
print(callable(Class2)) # Class2
print(callable(Class1())) # Class1 object
print(callable(Class2())) # Class2 object
print(callable(func)) # func
然后,只有没有__call__()的Class2对象是不可调用的,返回False,如下所示:
True # Class1
True # Class2
True # Class1 object
False # Class2 object
True # func
此外,下面所有的都是不可调用的,返回False,如下所示:
print(callable("Hello")) # "str" type
print(callable(100)) # "int" type
print(callable(100.23)) # "float" type
print(callable(100 + 2j)) # "complex" type
print(callable(True)) # "bool" type
print(callable(None)) # "NoneType"
print(callable([])) # "list" type
print(callable(())) # "tuple" type
print(callable({})) # "dict" type
print(callable({""})) # "set" type
输出:
False # "str" type
False # "int" type
False # "float" type
False # "complex" type
False # "bool" type
False # "NoneType"
False # "list" type
False # "tuple" type
False # "dict" type
False # "set" type
可调用对象是任何可以调用的对象。
内置可调用对象(objects.c中的PyCallable_Check)检查参数是否为:
类的实例,具有__call__方法或 具有非空tp_call (c struct)成员的类型,该成员指示可调用性(例如在函数、方法等中)。
名为__call__的方法是(根据文档)
当实例作为函数被“调用”时调用
例子
class Foo:
def __call__(self):
print 'called'
foo_instance = Foo()
foo_instance() #this is calling the __call__ method