现在,元类是什么已经很清楚了,有一个相关的概念,我一直在使用,但不知道它的真正含义。

我想每个人都犯过一次圆括号错误,导致“对象不可调用”异常。更重要的是,使用__init__和__new__会导致想知道这个该死的__call__可以用来做什么。

你能给我一些解释吗,包括魔术方法的例子?


当前回答

可调用对象是允许您使用圆括号()并最终传递一些参数的对象,就像函数一样。

每次定义函数时,python都会创建一个可调用对象。 例如,你可以用这些方式定义函数func(它们是一样的):

class a(object):
    def __call__(self, *args):
        print 'Hello'

func = a()

# or ... 
def func(*args):
    print 'Hello'

你可以使用这个方法而不是doit或run方法,我认为obj()比obj.doit()更清楚。

其他回答

你可以在它后面加上“(args)”,并期望它能工作。可调用对象通常是一个方法或类。方法被调用,类被实例化。

在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

具有__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

Callable是带有方法的“内置函数或方法”的类型或类 调用

>>> type(callable)
<class 'builtin_function_or_method'>
>>>

例子: Print是一个可调用对象。使用内置函数调用 当你调用print函数时,Python创建一个print类型的对象,并调用它的方法调用,如果有参数,则传递参数。

>>> type(print)
<class 'builtin_function_or_method'>
>>> print.__call__(10)
10
>>> print(10)
10
>>>

可调用对象实现了__call__特殊方法,因此任何具有这种方法的对象都是可调用的。