User Tools

Site Tools


python:metaclass:tutorial1

What is metaclass in Python

Metaclasses seem to be very complicated at first glance. Some people says that if you can solve your python programming problems with other methods you do not need to use metaclasses. If you have such a mechanism in your favorite programmng language python, why do not you use it? In my opinion, if you do not dive into this subject you will not be very good python programmer. This tutorial is based on Python 2.7!

You may also be interested in my class factory tutorial to understand the __new__ , __init__ and __call__ magical methods.


Let's first start with the definition of metaclass

A metaclass is the class of a class

type()

Which means, when you create a class in your code, class's and its instances' behavioura are defined by its metaclass. That is to say, your class is an instance of metaclass and metaclass is factory to produce your classes. type is common metaclass for all classes. You most probably used type for other purposes like;

>>> type(1)
<type 'int'>
>>> type("some string")
<type 'str'>
>>> type(u"some string")
<type 'unicode'>
>>> type(u"some string") is unicode
True
>>> type(10.1) is int
False
>>> type(10.1) is float
True

More

>>> import types
>>> type(1) is types.IntType
True
>>> type((i for i in range(10))) is types.GeneratorType
True
>>> type(lambda:None) is types.LambdaType
True

type has more features than this when you use metaclasses. By the way isintance() is the prefered way for type checking!

As Python 2 documentation says that


class type(object)
class type(name, bases, dict)
With one argument, return the type of an object. The return value is a type object.

We deal with the second form of the type It is documented as below


With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the __dict__ attribute. For example, the following two statements create identical type objects:

>>> class X(object):
...     a = 1
...
>>> X = type('X', (object,), dict(a=1))
>>> X
<class '__main__.X'>

So, as you see type produces classes if you give class name, base classes and the attributes as dictionary.

Back to metaclass

instance is instance of class and class is instance of metaclass

This code shows the relationship defined;

>>> class X(object):
	a=1
 
>>> type(X)
<type 'type'>
>>> X
<class '__main__.X'>
>>> x=X()
>>> type(x)
<class '__main__.X'>
>>> x
<__main__.X object at 0x02BA0F30>
>>> type(X)
<type 'type'>

If you are new to metaclasses you still ask yourself how and where you use this. Well, it highly depends on your imagination however I will give you some examples.

TO BE CONTINUED…

Discussion

Enter your comment. Wiki syntax is allowed:
 
python/metaclass/tutorial1.txt · Last modified: 2015/12/26 11:17 (external edit)