__call__
Posted saolv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了__call__相关的知识,希望对你有一定的参考价值。
object.__call__(self[, args...])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).
Python中有一个有趣的语法,只要定义类型的时候,实现__call__
函数,这个类型就成为可调用的。换句话说,我们可以把这个类的对象当作函数来使用,相当于重载了括号运算符。
class Student(object):
def __init__(self, name):
self.name = name
def __call__(self):
print(‘My name is %s.‘ % self.name)
s = Student(‘Michael‘)
s()
# My name is Michael.
通过使用__setattr__
, __getattr__
, __delattr__
可以重写dict,使之通过“.”调用键值。
class Dict(dict):
‘‘‘
通过使用__setattr__,__getattr__,__delattr__
可以重写dict,使之通过“.”调用
‘‘‘
def __setattr__(self, key, value):
print("In ‘__setattr__")
self[key] = value
def __getattr__(self, key):
try:
print("In ‘__getattr__")
return self[key]
except KeyError as k:
return None
def __delattr__(self, key):
try:
del self[key]
except KeyError as k:
return None
# __call__方法用于实例自身的调用,达到()调用的效果
def __call__(self, key): # 带参数key的__call__方法
try:
print("In ‘__call__‘")
return self[key]
except KeyError as k:
return "In ‘__call__‘ error"
s = Dict()
print(s.__dict__)
# {}
s.name = "hello" # 调用__setattr__
# In ‘__setattr__
print(s.__dict__) # 由于调用的‘__setattr__‘, name属性没有加入实例属性字典中。
# {}
print(s("name")) # 调用__call__
# In ‘__call__‘
# hello
print(s["name"]) # dict默认行为
# hello
# print(s)
print(s.name) # 调用__getattr__
# In ‘__getattr__
# hello
del s.name # 调用__delattr__
print(s("name")) # 调用__call__
# None
以上是关于__call__的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError : 类实例没有 __call__ 方法