python(__getitem__、__setitem__、__delitem__)

# Author:Sooele

class Foo(object):
    def __init__(self):
        self.data = {}
    def __getitem__(self, key):
        print('__getitem__', key)
        return self.data.get(key)

    def __setitem__(self, key, value):
        print('__setitem__', key, value)
        self.data[key] = value

    def __delitem__(self, key):
        print('__delitem__', key)


obj = Foo()
obj['name'] = "Sooele"
print(obj['name'])
#
# result = obj['k1']  # 自动触发执行 __getitem__
# obj['k2'] = 'alex'  # 自动触发执行 __setitem__
# del obj['k1']

python(__getitem__、__setitem__、__delitem__)》有1个想法

发表评论