The __dict__ attribute

Classes, instances, and modules are built using dictionaries

  • The dictionary is found in the __dict__ attribute
     import string
     print string.__dict__       # Print out the module dictionary
     
     print Account.__dict__      # Output the dictionary of a class
     a = Account()
     print a.__dict__            # Output the dictionary of an instance

Attribute lookup and dictionaries

  • All operations of the form obj.name are translated into dictionary operations.
     a = obj.name                # a = obj.__dict__[name]
     obj.name = x                # obj.__dict__[name] = x 
  • Caveat: Different types may perform additional processing behind the scenes.
  • Example, lookups on a class instance will search through the dictionaries of base classes.
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 44
July 17, 2000, beazley@cs.uchicago.edu
>>>