Home arrow Python arrow Page 2 - Metaclasses: Blueprints of Blueprints

The Barebones - Python

We all know that objects are created from classes. But can a class be created from a class? Yes it can; this is called a metaclass. They give us a great deal of power when it comes to changing the behavior of a class. Python 2.2 supports metaclasses. Peyton McCullough explains.

  1. Metaclasses: Blueprints of Blueprints
  2. The Barebones
  3. Adding Some Meat
  4. Using Metaclasses
  5. A class named G
By: Peyton McCullough
Rating: starstarstarstarstar / 13
December 13, 2005

print this article



Metaclasses are created just like classes, except they are a subclass of type or another metaclass. Actually, type is itself a metaclass. There isn't any interesting syntax involved with their creation:

>>> class MetaClass ( type ):

Now, to demonstrate how we can use our overly-primitive metaclass, let's create a class from it. There are two ways to do this. The first way is to create a class from our metaclass just as we would create an object from a normal class. However, we have to make a few changes, or we'll end up with this:

>>> A = MetaClass()

Traceback (most recent call last):
  File "<pyshell#40>", line 1, in -toplevel-
    A = MetaClass()
TypeError: type() takes 1 or 3 arguments

Unless you instruct them to do otherwise, metaclasses accept three arguments: the name of the class to be created, a tuple of the base classes involved and a dictionary containing the class's attributes:

>>> A = MetaClass ( 'A', (), {} )
>>> A
<class '__main__.A'>

We can see our metaclass of our class like this:

>>> A.__class__
<class '__main__.MetaClass'>

Similarly, we can check the metaclass of an object like so:

>>> z = A()
>>> z.__class__.__class__
<class '__main__.MetaClass'>

Creating some attributes is not very complicated. Let's create a class with a few variables:

>>> B = MetaClass ( 'B', (), { 'p': 4, 'q': ( '1', 2, '3' ), 'r': "%" } )
>>> B.p
>>> B.q
('1', 2, '3')
>>> B.r

Of course, this is pretty annoying and pointless for anything complex, which leads us to the second method of creating classes from metaclasses. You can simply assign the metaclass to a new-style class's __metaclass__ variable:

>>> class C ( object ):
 __metaclass__ = MetaClass

This is a more practical method of creating classes from metaclasses than the previous method.

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: