Python
  Home arrow Python arrow Page 4 - 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 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Metaclasses: Blueprints of Blueprints - Using Metaclasses


    (Page 4 of 5 )

    You're probably wondering when and where to use metaclasses. While there is no rule that tells us exactly that, we can look over a few situations where metaclasses can be used.

    We'll start with a widely-used example, and then we'll build some features onto it to form more examples. Take a look at class E:

    >>> class E:
     pass

    If we try to print the class out, we get a string representation of E:

    >>> print E
    __main__.E

    What if we want to change the output to something more informative, though? This requires changing the behavior of the class. The words “changing the behavior” should have sounded an alarm in your mind. Metaclasses are perfect for changing the behavior of a class.

    If we were performing our work on an object, we would simply define a __str__ method in the parent class. Since we're working with a class, though, we need to define the method in the metaclass. The method needs to return a string with the description we want:

    >>> class MetaF ( type ):
     def __str__ ( cls ):
      return "A class made from metaclass MetaF"

    We have now achieved the desired behavior:

    >>> class F ( object ):
     __metaclass__ = MetaF


    >>> print F
    A class made from metaclass MetaF
    >>> str ( F )
    'A class made from metaclass MetaF'

    We can also go above and beyond, though. Instead of having a generic message to describe what may be an entire set of classes, we can give each class the capability to define a custom message. The message can be stored in a predefined variable that each class can change. This variable can be returned in our metaclass's __str__ method. Take a look at metaclass MetaG:

    >>> class MetaG ( type ):
     __nameString__ = 'A class made from metaclass MetaG'
     def __str__ ( cls ):
      return cls.__nameString__

    Each class made from MetaG will contain the variable __nameString__ and can change it. When we request a string representation of each class, its __nameString__ variable will then be returned:

    >>> class G ( object ):
     __metaclass__ = MetaG


    >>> print G

    A class made from metaclass MetaG

    >>> class G ( object ):
     __metaclass__ = MetaG
     __nameString__ = 'A class named G'


    >>> print G

    More Python Articles
    More By Peyton McCullough


       · 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