I would like to create a “class property” declared in an abstract base class and then overridden in a specific implementation class, while preserving the beautiful statement that the implementation should override the class property of the abstract base class.
Although I looked at this question , my naive attempt to reassign the accepted answer did not work:
>>> import abc >>> class ClassProperty(abc.abstractproperty): ... def __get__(self, cls, owner): ... return self.fget.__get__(None, owner)() ... >>> class Base(object): ... __metaclass__ = abc.ABCMeta ... @ClassProperty ... def foo(cls): ... raise NotImplementedError ... >>> class Impl(Base): ... @ClassProperty ... def foo(cls): ... return 5 ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/2rs2ts/src/myproj/env/lib/python2.7/abc.py", line 94, in __new__ value = getattr(cls, name, None) File "<stdin>", line 3, in __get__ TypeError: Error when calling the metaclass bases unbound method foo() must be called with Impl instance as first argument (got nothing instead)
I lost a little what I have to do. Any help?
python abc
2rs2ts
source share