CONSTRUCT - Adding Functionality to the Overall System

Georg Brandl g.brandl-nospam at gmx.net
Tue Sep 19 04:46:48 EDT 2006


Diez B. Roggisch wrote:
>>>
>>> I think '__metaclass__ = whatever' affects only the creation of 
>>> classes that
>>> would otherwise be old-style classes?
>> 
>> Wrong.
>> 
>> If you set __metaclass__ = type, every class in that module will be 
>> new-style.
>> 
>> If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
>> class in that module will be new-style and have MyClass as a metaclass.
>> 
>> The usual way to create new-style classes, inheriting from object or 
>> another
>> new-style class, works because if no __metaclass__ is defined, the first
>> base class's class is taken as the metaclass.
> 
> 
> I was under that impression, too. But this behaves different (py2.4):

[...]

> deets$ python2.4 /tmp/test.py
> (<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})
> deets$
> 
> I was astonished to see that. Any explanation?

Yes. I was wrong, the order of lookup is reversed. This first base class's
class is used before the global __metaclass__.

So what Damjan said is correct: global __metaclass__ is only usable for
classes that don't have bases.

Whether this is desirable is another question...

Georg



More information about the Python-list mailing list