Python metaclasses, which function as dynamic class generators or colloquially as class factories, provide a level of aspect-oriented programming to the Python language by allowing for class and instance construction to cut across multiple abstractions within a code base. Metaclasses also serve more traditional object-oriented interests in Python by exposing functionality for convenient class decoration types of operations without having rely on inheritance or extensive explicit decoration.
At Vizme, we use metaclasses for both class definition as part of clean object-oriented implementations, but also more interestingly as aspect-oriented factories for allowing classes in our codebase to serve multiple interests and use cases depending on the environment and settings of hosting processes; a technique that is also being used in frameworks like Django and Pyramid that have similar needs. The most interesting example of which is Vizme’s use of metaclasses to make our SQLAlchemy Object Relational Models (ORMs) function on a multi-server, sharded and replicated database cloud. With metaclasses we serve multiple interests, e.g. slaves, masters, web accessed entry points, and internal server spawned processes, all with a single class hierarchy by defining custom metaclasses that generate classes dynamically to suit these various constraints.
This session will introduce metaclasses and the concepts necessary to use them. We will then attempt to remove some of the uncertainty in metaclass implementations through discussion of good and bad uses for metaclasses. Finally, we’ll explore at a high level actual implementations of metaclasses as they are used within the Vizme code base.
Eric D. Wills is an Instructor in the department of Computer and Information Science (CIS) at the University of Oregon and Director of R%D at Vizme. He received his Ph.D. in CIS from the University of Oregon in 2008. His teaching and research interests include 3D graphics and animation, computer architecture, and distributed server-side computing.
For information on exhibition and sponsorship opportunities at the conference, contact Sharon Cordesse at (707) 827-7065 or firstname.lastname@example.org.
Download the OSCON Sponsor/Exhibitor Prospectus.
View a complete list of OSCON contacts