码迷,mamicode.com
首页 > 其他好文 > 详细

__call__

时间:2018-10-23 22:52:46      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:rip   erro   title   方法   自身   val   语法   括号运算符   student   

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__

标签:rip   erro   title   方法   自身   val   语法   括号运算符   student   

原文地址:https://www.cnblogs.com/saolv/p/9839523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!