Metaclasses: Blueprints of Blueprints - A class named G
(Page 5 of 5 )
What if we wanted to return a “map” of a class' attributes? We would need to loop through the attribute dictionary and format the contents. Instead of redoing the __str__ method, though, we will redo __repr__. This way, the contents of an instance can be spilled out in the command line by just typing the instance class' name, rather than attaching print. MetaH does all of this for us:
>>> import types
>>> class MetaH ( type ):
# Create a dictionary variable to store the attributes in
# We want to use the attribute dictionary in __repr__
dct = None
def __init__ ( cls, name, bases, dct ):
cls.dct = dct
def __repr__ ( cls ):
# Create a list to store methods in
methods = []
# Create another to store variables in
variables = []
# Loop through the attributes and add them
for key, value in cls.dct.iteritems():
if type ( value ) == types.FunctionType:
methods.append ( key )
else:
variables.append ( key )
# Sort the lists
methods.sort()
variables.sort()
# Create a string to store the "map" in
map = "Class Map"
# Put everything in the map
map = map + "\n\nMETHODS:"
for method in methods:
map = map + "\n" + method
map = map + "\n\nVARIABLES:"
for variable in variables:
map = map + "\n" + variable
# Return the map
return map
The first thing that MetaH does is create a variable to house the dictionary of attributes. The dictionary of attributes is moved to the variable in the __init__ method. We then create lists to store the class' methods and variables in the __repr__ method, and we loop through the dictionary to see what attribute belongs where. Finally, we sort the lists, create a string and then return that string. Here's our metaclass in action:
>>> class H ( object ):
__metaclass__ = MetaH
a = 1
b = 2
c = 3
def d ( self ):
pass
def e ( self ):
pass
def f ( self ):
pass
>>> H
Class Map
METHODS:
d
e
f
VARIABLES:
__metaclass__
__module__
a
b
c
Wrapping It Up
You should now know what metaclasses are and should have an idea of what situations call for the use of metaclasses. Metaclasses are, in the simplest definition, blueprints of blueprints. The relationship between a metaclass and a class is just like the relationship between a class and an object. A class changes the behavior of an object, and a metaclass changes the behavior of a class.
Although metaclasses are not used very often, they are powerful devices when they are used. They can change the internals of a class, affecting its behavior in ways not normally possible. They can also be used to generate classes dynamically, just as objects can be created dynamically from classes.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |