现在,元类是什么已经很清楚了,有一个相关的概念,我一直在使用,但不知道它的真正含义。
我想每个人都犯过一次圆括号错误,导致“对象不可调用”异常。更重要的是,使用__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都会创建一个可调用对象。 例如,你可以用这些方式定义函数func(它们是一样的):
class a(object):
def __call__(self, *args):
print 'Hello'
func = a()
# or ...
def func(*args):
print 'Hello'
你可以使用这个方法而不是doit或run方法,我认为obj()比obj.doit()更清楚。
检查类的函数或方法是否可调用,这意味着我们可以调用该函数。
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
你可以在它后面加上“(args)”,并期望它能工作。可调用对象通常是一个方法或类。方法被调用,类被实例化。
可调用对象实现了__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