using metaclass __call__ to replace class instance

Peter Otten __peter__ at web.de
Fri Sep 9 14:24:46 CEST 2005


Ksenia Marasanova wrote:

> class BasemethodMeta(type):
>     def __new__(cls, class_name, bases, new_attrs):
>         cls = type.__new__(cls, class_name, bases, new_attrs)
>         new_attrs['__metaclass__'].cls = cls
>         return cls
> 
>     def __call__(self):
>         return self.cls.get_foo()

Though I'm not sure what you are trying to do, I fear it will get more
complicated than necessary. But you do get the desired output if
you change your metaclass to

class BasemethodMeta(type):
    def __call__(cls):
        # the instance of the metaclass already is a class
        # so you can drop the double indirection
        return cls.get_foo()

Peter





More information about the Python-list mailing list