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

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

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


当前回答

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

其他回答

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

__call__使任何对象都可以作为函数调用。

这个例子将输出8:

class Adder(object):
  def __init__(self, val):
    self.val = val

  def __call__(self, val):
    return self.val + val

func = Adder(5)
print func(3)

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

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

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

func = a()

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

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

让我反过来解释:

考虑这个……

foo()

... 作为语法上的糖:

foo.__call__()

其中foo可以是任何响应__call__的对象。当我说任何对象时,我指的是内置类型,你自己的类和它们的实例。

对于内置类型,当你写:

int('10')
unicode(10)

你实际上是在做:

int.__call__('10')
unicode.__call__(10)

这也是Python中没有foo = new int的原因:你只需要让类对象在__call__上返回它的一个实例。在我看来,Python解决这个问题的方式非常优雅。

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