How to catch a particular name assignment?
问题 (Based on this question): One can override the __setattr__ magic method for an object to have additional instructions when an attribute of an object is set. As in: class MyClass(object): def __init__(self, attribute=None): object.__init__(self) self.attribute = attribute def __setattr__(self, name, value): self.__dict__[name] = value if name == 'attribute': print("attribute's value is modified to {}.".format( self.attribute)) if __name__ == '__main__': my_obj = MyClass(True) while True: my