using metaclass __call__ to replace class instance
Ksenia Marasanova
ksenia.marasanova at gmail.com
Fri Sep 9 08:21:29 EDT 2005
2005/9/9, Ksenia Marasanova <ksenia.marasanova at gmail.com>:
> 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()
>
> class Basemethod(object):
> __metaclass__ = BasemethodMeta
> def get_foo(cls):
> return cls.foo
> get_foo = classmethod(get_foo)
>
>
> But it doesn't work as I expected:
>
> print Class.method1()
> "foo2"
> print Class.method2()
> "foo2"
As usual, it helps to write things down :)
I found an alternative way, probably a lot easier to maintain: return
*instance* of Basemethod class in __new__ method of the
BasemethodMeta, and then use __call__ method of Basemethod class for
all the things.
Python is such fun...
--
Ksenia
More information about the Python-list
mailing list