__call__ 、__getitem__ & __setitem__

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32023541/article/details/80321841
class Example(object):
    def __init__(self):
        self.kk = {}
    def __call__(self,id):
        self.kk["id"] = id
    def __getitem__(self,id):
        return self.kk[id]
    def __setitem__(self,id,value):
        self.kk[id] = value
example = Example()
example["name"] = "oyp"  # 相当于 example.__setitem__("name","oyp")
print example["name"]    # 相当于 print example.__getitem__("name")
example(123)             # 相当于 example.__call__(123)
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页