# descrip.py # # An example of defining a descriptor object. In this descriptor, functions # actually store, retrieve, and delete data from the instance dictionary. # The only odd part is that the descriptor needs to know what name to use # for these operations. If you look at the class Foo below, you'll see # names given to the descriptor object. class Descriptor(object): def __init__(self,name): self.name = name def __get__(self,instance,cls): print "getting", self.name return instance.__dict__[self.name] def __set__(self,instance,value): print "setting", self.name, value instance.__dict__[self.name] = value def __delete__(self,instance): print "deleting", self.name del instance.__dict__[self.name] class Foo(object): x = Descriptor("x") y = Descriptor("y") # Example f = Foo() f.x = 2 f.y = 3 print f.x print f.y