SunQuest
 
       Python
  Home arrow Python arrow Page 5 - Metaclasses: Blueprints of Blueprints
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Metaclasses: Blueprints of Blueprints
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-12-13

    Table of Contents:
  • Metaclasses: Blueprints of Blueprints
  • The Barebones
  • Adding Some Meat
  • Using Metaclasses
  • A class named G

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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.

       · This is the metaclasses article that Peyton McCullough mentioned last week. I hope...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway