2

How do I mark an instance attribute as not implemented in a base class? (Distinct from this question which discusses marking class attributes as not implemented, but perhaps I'm not understanding base classes correctly...)

e.g. I want something like

class Base():
    def __init__(self):
        self.x = NotImplemented

class GoodSub(Base):
    def __init__(self, x):
        super().__init__()
        self.x = x #good

class BadSub(Base):
    def __init__(self):
       super().__init__()
       #forgot to set self.x

good = GoodSub(5)
bad = BadSub(-1)    
good.x #returns 5
bad.x #throws error because x not implemented

Or, is there a better way to enforce all subclasses of Base to set the self.x attribute upon init?

Edit: link to related question

dkv
  • 6,602
  • 10
  • 34
  • 54
  • 1
    note that `bad.x` does not throw an error, it returns the class `NotImplemented`. – Adam Smith Jul 11 '19 at 21:02
  • Yes, this is exactly what I'm trying to get around. Using `@property`s feels a bit heavy-handed, especially if there are many "abstract instance attributes" I would like to define in the base class... – dkv Jul 11 '19 at 21:11

2 Answers2

3

One solution with class decorators and descriptors (__get__ method):

def abstract_variables(*args):
    class av:
        def __init__(self, error_message):
            self.error_message = error_message

        def __get__(self, *args, **kwargs):
            raise NotImplementedError(self.error_message)

    def f(klass):
        for arg in args:
            setattr(klass, arg, av('Descendants must set variable `{}`'.format(arg)))
        return klass

    return f


@abstract_variables('x', 'y')
class Base:
    def __init__(self):
        pass

class Derived(Base):
    x = 10


b = Base()
d = Derived()
print(d.x)    # prints 10
print(d.y)    # raises NotImplementedError

Prints:

10
Traceback (most recent call last):
  File "main.py", line 28, in <module>
    print(d.y)
  File "main.py", line 7, in __get__
    raise NotImplementedError(self.error_message)
NotImplementedError: Descendants must set variable `y`
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
2

I'd consider making x a property.

class Base():
    def __init__(self):
        self.__x = None
        self.__x_is_set = False
    @property
    def x(self):
        if not self.__x_is_set:
            raise NotImplementedError('Descendents from Base must set x')
        else:
            return self.__x
    @x.setter
    def x(self, value):
        self.__x = value
        self.__x_is_set = True


class GoodSub(Base):
    def __init__(self):
        super().__init__()
        self.x = 5

class BadSub(Base):
    def __init__(self):
        super().__init__()
        pass

class AlsoBad(Base):
    def __init__(self):
        super().__init__()
        self.__x = 5  # sets the attribute, but not through the property
>>> g, a, b = GoodSub(), BadSub(), AlsoBad()
>>> g.x
5
>>> a.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in x
NotImplementedError: Descendents from Base must set x
>>> b.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in x
NotImplementedError: Descendents from Base must set x
Adam Smith
  • 52,157
  • 12
  • 73
  • 112