CONSTRUCT - Adding Functionality to the Overall System

Diez B. Roggisch deets at
Tue Sep 19 10:16:36 CEST 2006

>> 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):

---- ----
class meta(type):
     def __new__(*args):
         print args
         return type(*args[1:])

__metaclass__ = meta

class OldStyle:

class NewStyle(object):
     #__metaclass__ = meta


---- ----

deets$ python2.4 /tmp/
(<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})

I was astonished to see that. Any explanation?


More information about the Python-list mailing list