Object-oriented methods --item python

class Foo:
    def __getitem__(self, item):
        print("getitem")
        return self.__dict__[item]

    def __setitem__(self, key, value):
        print("setitem")
        self.__dict__[key]=value

    def __delitem__(self, key):
        print("delitem")
        self.__dict__.pop(key)

f1=Foo()
Print (F1. the __dict__ )
 # f1.name = "Egon" 
F1 [ ' name ' ] = " Egon " 
F1 [ " Age " ] = " 20 is " 

Print (F1. the __dict__ ) 

del F1 [ " name " ]
 Print (F1 . the __dict__ ) 

F1 [ ' Age ' ]
 Print (F1 [ ' Age ' ]) 

# attribute mode associated with the operation point getattr, attribute item related to the operation of the brackets
Note #item method is accessed through the index dictionary, getattr through. No. instance properties, __ getitem__ method only when the property will be triggered by the presence of self when the index visit, __ getattr__ self when accessing the property does not exist when the trigger

 

Guess you like

Origin www.cnblogs.com/tangcode/p/11390325.html