__metaclass__ and __author__ are already decorators

Lenard Lindstrom len-1 at telus.net
Sat Aug 21 19:24:35 CEST 2004


Andrew Durdin <adurdin at gmail.com> writes:

> __metaclass__ on the other hand is a magic attribute. I haven't worked
> out the details, but I have the feeling that what a metaclass does to
> a class is not replicable merely by decorating the class; in other
> words, that:
> 
>   class Foo:
>       __metaclass__ = Bar
> 
> is *not* equivalent to
> 
>   class Foo:
>       pass
> 
>   Foo = Baz(Foo)
> 
> for any definitions of Bar and Baz. This is just my intuition, however.

Used within a class decoration __metaclass__ is just another class attribute,
but it does have special meaning to the class factory. It names a new factory
to use in place of the default one. And its effects are inherited. So for:

class SubFoo(Foo):
    ...

SubFoo also gets metaclass Bar. Foo = Baz(Foo) would not work that way.
And yes Bar and Baz would be different. Bar would take three arguments, a
class name, a list of base classes, and a dictionary of class attributes.
Baz would extract this information from an existing class object.

Lenard Lindstrom
<len-l at telus.net>





More information about the Python-list mailing list