[Python-3000] Metaclass syntax?

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed May 3 00:39:38 CEST 2006


Jonathan LaCour wrote:

> Guido van Rossum wrote:
>> This has usually been solved by creating a non-meta class whose only
>> purpose is to introduce the new metaclass. You'd write
>> 
>> class TheLarch(KindsOfTrees, deciduous): ...
>> 
>> and the metaclass would be called KindsOfTreesMeta.
> 
>      def of_type(metaclass):
>          class MetaclassMixin(object):
>              __metaclass__ = metaclass
>          return MetaclassMixin
> 
> ... and then I simply use this on my classes when I define them:
> 
>      class MyClass(of_type(SomeMetaclass)):

"of_type" doesn't really work for me - why not just name it "metaclass"?

    def metaclass(meta):
        class MetaclassMixin(object):
            __metaclass__ = metaclass
        return MetaclassMixin

    class MyClass(metaclass(SomeMetaclass)):
        pass

Off the top of my head, I can't see any reason this wouldn't work in the
case of multiple compatible metaclasses (due to inheritance).

Tim Delaney


More information about the Python-3000 mailing list