CONSTRUCT - Adding Functionality to the Overall System

Diez B. Roggisch deets at nospam.web.de
Tue Sep 19 04:16:36 EDT 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):


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

__metaclass__ = meta

class OldStyle:
     pass

class NewStyle(object):
     #__metaclass__ = meta

     pass



---- test.py ----

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

I was astonished to see that. Any explanation?

Diez



More information about the Python-list mailing list