Trying an implementation where a base abstract class implements several properties (building some paths). With specific implementations having specific parts of the paths.

See a simplified example below. I have tried this with abstract properties, but am getting TypeError.

The base_prop is being called from the base class but it is from an instance of the specific class, yet doesn't see the specific prop. I thought it should know it, no?

import abc


class MyBase(metaclass=abc.ABCMeta):
    @property
    @abc.abstractmethod
    def specific_prop(self):
        raise NotImplementedError

    @property
    def base_prop(self):
        return f'{self.specific_prop} CHZBRGR?'


class MySpecific(MyBase):
    def specific_prop(self):
        return "I CAN HAS "


myspec = MySpecific()
asd = myspec.base_prop()
print(asd)  # expected to return "I CAN HAS CHZBRGR?" but explodes with TypeError: 'str' object is not callable
1

There are 1 best solutions below

0
mrc On

Thanks to @Clasherkasten comment, fixed code:

import abc


class MyBase(metaclass=abc.ABCMeta):
    @property
    @abc.abstractmethod
    def specific_prop(self):
        raise NotImplementedError

    @property
    def base_prop(self):
        return f'{self.specific_prop} CHZBRGR?'


class MySpecific(MyBase):
    @property                   # THIS IS ADDED
    def specific_prop(self):
        return "I CAN HAS "


myspec = MySpecific()
asd = myspec.base_prop          # NO PARENTHESES HERE
print(asd)