Slight metaclass confusion

Stephan Diehl stephan.diehl at gmx.net
Tue Sep 9 14:38:32 CEST 2003


Ben Young wrote:


> [snip]
>>
>> The problem is perhaps the terminology (Unfortunatelly,I don't know a
>> better one).
>>
>> The metaclass initializes a class. Just imagine, that the textual class
>> definition comes fresh from the parser, so you have information about
>> possible base classes, methods, attributes. These information can be
>> changed within the metaclass __new__ method.
>> Think of the metaclass as a factory that turns a class definition into a
>> class instance.
>> Other than that, they don't interact.
>>
>> What a metaclass can do, is to change that information, before the class
>> is made into a class object.
>>
>> Basicly, the metaclass concept is orthogonal to the inheritence concept
>> of OOP.
>>
> 
> So the way it is implemented in python a class is NOT an instance of a
> metaclass the same way that an instance is an instance of a class. This is
> slightly confusing as this is not the way it is described in all the
> places I can find that talk about python metaclasses.

For all I know, a class is the instance of its metaclass, but this doesn't
mean that an object a of class A can access methods of A' (the metaclass of
A). (Please note that this is probably totally wrong. Other people might
shed light on this more theoretical question)

> 
> In this case, what is the point of using a class when a simple function
> can do the job?

Well, actually, there is probably nothing you can do with metaclasses that
you couldn't do in other ways.

The point (if you are not a language lawyer) is that the resulting code is
(at least in some cases) better readable when using metaclasses.

The Python cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python) has a
couple of recipies about metaclasses. Please have a look there if you are
interested in practical things you can do with metaclasses.

> 
>> Hope that helps
>>
>> Stephan
>>
>>
> 
> Thanks for the reply
> 
> Ben
> ---





More information about the Python-list mailing list