An interactive session shows that the lookup is dynamic — it computes different, updated answers each time: >>> s =Directory('songs') >>> g =Directory('games') >>> s.size # The songs directory has twenty files 20 >>> g.size # The games directory has three files 3 >>> os.remove( 'games/chess') # Delete a game >>> g.size # File count is automatically updated 2 import logging logging.basicConfig(level =logging.INFO) classLoggedAgeAccess : def __get__ (self, obj, objtype = None ): value=obj._age logging.info('Accessing %r giving %r ','age', value) return value def __set__ (self, obj, value): logging.info('Updating %r to %r ','age', value) obj._age=value classPerson : age =LoggedAgeAccess() # Descriptor instance def __init__ (self, name, age): self.name =name # Regular instance attribute self.age =age # Calls __set__() def birthday (self): self.age +=1 # Calls both __get__() and __set__() >>> mary =Person('Mary M ',30 ) # The initial age update is logged INFO:root:Updating 'age' to 30 >>> dave=Person('David D',40 ) INFO:root:Updating 'age' to 40 >>> vars(mary) # The actual data is in a private attribute (continues on next page) 4 An interactive session shows that all access to the managed attribute age is logged, but that the regular attribute name is not logged: In the following example, age is the public attribute and _age is the private attribute. When the public attribute is accessed, the descriptor logs the lookup or update: A popular use for descriptors is managing access to instance data. The descriptor is assigned to a public attribute in the class dictionary while the actual data is stored as a private attribute in the instance dictionary. The descriptor’s __get__() and$_{__set__()}$ methods are triggered when the public attribute is accessed. Besides showing how descriptors can run computations, this example also reveals the purpose of the parameters to __get__() . The self parameter is size , an instance of DirectorySize . The obj parameter is either g or s , an instance of Directory . It is the obj parameter that lets the __get__() method learn the target directory. The objtype parameter is the class Directory . 1.3 Managed attributes