Reprint: http://blog.csdn.net/xhw88398569/article/details/48690163
__xxxitem__: Called when manipulating properties using [']
__setitem__: This method is called whenever a property is assigned, so it cannot be assigned a value in the method Self.name = value will die loop
__getitem__: This method is called when a nonexistent property is accessed
__delitem__: This method is called when a property is deleted
Class A (object): def __init__ (self): self[' B '] = "BB" self[' d '] = "DD" del self[' d '] def __setitem __ (self,name,value): " @summary: The method is called whenever the property is assigned, so it cannot be assigned within the method Self.name = value will die loop ' print "__setitem__:set%s Value%s"% (Name,value) self.__dict__[name] = Value def __getitem__ (self,name): " ' @summary: Call the method ' print ' __getitem__:no attribute named '%s '%name return None when accessing a property that does not exist def __delitem__ (self,name): "' @summary: This method ' print ' is called when the property is deleted __delitem__:delect Attribute '%s ' "%name del self.__dict__[name] print self.__dict__ if __name__ = =" __main__ ": X = A ( b = x[' BB ')
Python __setitem__ (), __getitem__ (), __delitem__ ()