161

What is the shortest / most elegant way to implement the following Scala code with an abstract attribute in Python?

abstract class Controller {

    val path: String

}

A subclass of Controller is enforced to define "path" by the Scala compiler. A subclass would look like this:

class MyController extends Controller {

    override val path = "/home"

}
7
  • 1
    What have you tried? Please post your Python code with any problems or question you have about your solution.
    – S.Lott
    Apr 29, 2010 at 9:54
  • "A subclass of Controller is enforced to define "path" by the Scala compiler." ... Enforced when? If it's compile time, you're out of luck. If it's runtime, then how exactly do you want it "enforced"? In other words, is there a difference between raising an AttributeError and a NotImplementedError? Why?
    – detly
    Apr 29, 2010 at 10:38
  • 4
    I know that Python is a dynamic language and that the python interpreter cannot enforce static types. It is important to me, that it fails as early as possibly and that it is easy to find the place where the error orrured and why.
    – deamon
    Apr 29, 2010 at 11:30
  • Related: stackoverflow.com/questions/1151212/…
    – guettli
    Jun 16, 2016 at 14:33
  • 2
    There are some relevant answers to a newer duplicate: stackoverflow.com/questions/23831510/…, but basically the takeaway is that as of python 3.8 there is no nice solution.
    – Janus
    Jul 4, 2019 at 13:25

13 Answers 13

173

Python 3.3+

from abc import ABCMeta, abstractmethod


class A(metaclass=ABCMeta):
    def __init__(self):
        # ...
        pass

    @property
    @abstractmethod
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass

Failure to declare a or b in the derived class B will raise a TypeError such as:

TypeError: Can't instantiate abstract class B with abstract methods a

Python 2.7

There is an @abstractproperty decorator for this:

from abc import ABCMeta, abstractmethod, abstractproperty


class A:
    __metaclass__ = ABCMeta

    def __init__(self):
        # ...
        pass

    @abstractproperty
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass
8
  • My "A" class is a subclass of Exception, and this appears to break the example.
    – Chris2048
    Nov 13, 2018 at 11:11
  • 1
    @Chris2048 how does it break it? what error are you getting?
    – joel
    Jan 28, 2019 at 14:54
  • Is there a way to write this in a Python 2/3 agnostic way similar to this question?
    – bluenote10
    Feb 26, 2019 at 10:10
  • 26
    Your python 3 solution implements a as a class-property of B, instead of an object property. If you instead do ``` class B(A): def __init__(self): self.a=1 ... ``` you get an error when instantiating B: "Can't instantiate abstract class B with abstract methods a"
    – Janus
    Jul 4, 2019 at 13:19
  • 8
    This doesn't pass typechecking for me: literal cannot be assigned to declared type 'property'
    – rjh
    May 12, 2022 at 15:55
120

Since this question was originally asked, python has changed how abstract classes are implemented. I have used a slightly different approach using the abc.ABC formalism in python 3.6. Here I define the constant as a property which must be defined in each subclass.

from abc import ABC, abstractmethod


class Base(ABC):

    @classmethod
    @property
    @abstractmethod
    def CONSTANT(cls):
        raise NotImplementedError

    def print_constant(self):
        print(type(self).CONSTANT)


class Derived(Base):
    CONSTANT = 42

This forces the derived class to define the constant, or else a TypeError exception will be raised when you try to instantiate the subclass. When you want to use the constant for any functionality implemented in the abstract class, you must access the subclass constant by type(self).CONSTANT instead of just CONSTANT, since the value is undefined in the base class.

There are other ways to implement this, but I like this syntax as it seems to me the most plain and obvious for the reader.

The previous answers all touched useful points, but I feel the accepted answer does not directly answer the question because

  • The question asks for implementation in an abstract class, but the accepted answer does not follow the abstract formalism.
  • The question asks that implementation is enforced. I would argue that enforcement is stricter in this answer because it causes a runtime error when the subclass is instantiated if CONSTANT is not defined. The accepted answer allows the object to be instantiated and only throws an error when CONSTANT is accessed, making the enforcement less strict.

This is not to fault the original answers. Major changes to the abstract class syntax have occurred since they were posted, which in this case allow a neater and more functional implementation.

18
  • 4
    @BrianJoseph - The print_constant method is there to illustrate how to access the constant in the parent class, given that it is only defined in the child class
    – James
    Feb 22, 2020 at 5:33
  • 2
    What is the point of including the @classmethod decorator? Isn't this unnecessary?
    – YPCrumble
    Sep 2, 2020 at 14:46
  • 3
    self.CONSTANT will do as well, instead of type(self).CONSTANT. Dec 17, 2021 at 3:05
  • 3
    FYI: due to the way the decorators interact, @classmethod should be on top of @property
    – Mason3k
    Jan 11, 2022 at 18:24
  • 11
    As a note, stacking @classmethod and @property seems to be deprecated as of Python 3.11 (and yes, it was just introduced in Python 3.9). See this answer on a different question for a bit of discussion and some relevant links.
    – mitchnegus
    Mar 12, 2023 at 19:00
116

Python has a built-in exception for this, though you won't encounter the exception until runtime.

class Base(object):
    @property
    def path(self):
        raise NotImplementedError


class SubClass(Base):
    path = 'blah'
10
  • 40
    Specifically, you won't encounter the exception until the attrtibute is accessed, in which case you would have got an AttributeError anyway.
    – Ben James
    Apr 29, 2010 at 10:16
  • 8
    I think that raising a NotImplementedError is more explicit and therefore probably better than leaving it to an AttributeError.
    – blokeley
    Apr 29, 2010 at 12:48
  • 16
    Not that this only works if path is set directly on the SubClass. Given an instance sc = SubClass(), if you try to set sc.path = 'blah' or have a method that contains something like self.path = 'blah' without defining path directly on SubClass, you will get an AttributeError: can't set attribute.
    – erik
    Mar 31, 2015 at 16:59
  • 7
    It is not the answer!! This is not instance field. Scala filed in class is instance field because Scala separate the static and instance. Feb 28, 2019 at 11:05
  • 4
    @Simmovation see stackoverflow.com/a/16707102/308451
    – JBSnorro
    Apr 20, 2021 at 18:24
82

In Python 3.6+, you can annotate an attribute of an abstract class (or any variable) without providing a value for that attribute.

from abc import ABC

class Controller(ABC):
    path: str

class MyController(Controller):
    def __init__(self, path: str):
        self.path = path

This makes for very clean code where it is obvious that the attribute is abstract.

It should be noted that this will not raise an exception at definition time if a subclass does not provide an implementation. However, an AttributeError exception will be raised if anythhing tries to access the undefined attribute.

5
  • 1
    In Python 3.7 i dont get any errors if MyController is define without path property. Why?
    – DaniTeba
    Feb 23, 2021 at 9:20
  • 14
    @DaniTeba This solution raises and error at access time, not definition time. You would have to use one of the decorator solutions if raising the error at class definition time is important to you.
    – drhagen
    Feb 23, 2021 at 13:40
  • Thanks, this is a great solution that passes type-checking too.
    – rjh
    May 12, 2022 at 15:57
  • 8
    This is not appropriate. These are completely different categories of attributes. Controller.path is an attribute of the class, this shared among instances (equivalent to static attributes in C#). MyController().path is an atribute of the instance. Dec 19, 2022 at 14:21
  • 1
    @EduardoPignatelli Are you sure? I'm using Python 3.9 with the example above, and I can see that path is an annotation of the Controller class (in Controller.__dict__), but trying to use Controller.path is an attribute error: AttributeError: type object 'Controller' has no attribute 'path'. Even if the path assignment was removed in the MyController init method, instances of it still wouldn't have the path attribute and would raise the same attribute error
    – Bilbottom
    Aug 24, 2023 at 16:55
32

You could create an attribute in the abc.ABC abstract base class with a value such as NotImplemented so that if the attribute is not overriden and then used, a clear error that expresses intent is shown at run time.

The following code uses a PEP 484 type hint to help PyCharm correctly statically analyze the type of the path attribute as well.

from abc import ABC

class Controller(ABC):
    path: str = NotImplemented

class MyController(Controller):
    path = "/home"
4
  • 2
    Note this is not instance field. The Scala field is always instance member in the class definition. Because Scala separate the static and instance. Feb 28, 2019 at 11:01
  • 1
    Note this is abusing NotImplemented that is there for a different purpose. This was my idea too, so I kind of approve though ^^ I think @drhagen's answer is more correct, but using = NotImplemented is way more visible.
    – Ctrl-C
    Apr 23, 2021 at 18:35
  • 1
    Correct me if I'm wrong, but even without this, if an attribute is used and not overriden or defined, it will throw an error. It's basic behaviour for any class.
    – Corel
    May 25, 2021 at 21:18
  • @Corel the idea is that this makes the intent very explicit and discoverable.
    – johnthagen
    May 26, 2021 at 12:19
26

As of Python 3.6 you can use __init_subclass__ to check for the class variables of the child class upon initialisation:

from abc import ABC

class A(ABC):
    @classmethod
    def __init_subclass__(cls):
        required_class_variables = [
            'foo',
            'bar',
        ]
        for var in required_class_variables:
            if not hasattr(cls, var):
                raise NotImplementedError(
                    f'Class {cls} lacks required `{var}` class attribute'
                )

This raises an Error on initialisation of the child class, if the missing class variable is not defined, so you don't have to wait until the missing class variable would be accessed.

3
  • __init_subclass__ of the parent class is called before the subclass is initialised. So if you want to check for properties set in the subclass's __init__, this won't work.
    – anna_hope
    Nov 22, 2019 at 22:42
  • 6
    @notnami That is true. However, the question asks for class variables, not for instance variables which would be set during the __init__ method. Nov 24, 2019 at 21:31
  • 5
    Hum... Is it me or are you the only one to strictly address the question ? :) +1.
    – keepAlive
    Jul 4, 2020 at 10:35
25

For Python 3.3+ there's an elegant solution

from abc import ABC, abstractmethod
    
class BaseController(ABC):
    @property
    @abstractmethod
    def path(self) -> str:
        ...

class Controller(BaseController):
    path = "/home"


# Instead of an elipsis, you can add a docstring for clarity
class AnotherBaseController(ABC):
    @property
    @abstractmethod
    def path(self) -> str:
        """
        :return: the url path of this controller
        """

Despite some great answers have already been given, I thought this answer would nevertheless add some value. This approach has two advantages:

  1. ... in an abstract method's body is more preferable than pass. Unlike pass, ... implies no operations, where pass only means the absence of an actual implementation

  2. ... is more recommended than throwing NotImplementedError(...). This automatically prompts an extremely verbose error if the implementation of an abstract field is missing in a subclass. In contrast, NotImplementedError itself doesn't tell why the implementation is missing. Moreover, it requires manual labor to actually raise it.

4
  • 3
    Your point #2 is not correct. Whether you type ... or raise NotImplementedError or simply pass has no impact on the error raised when trying to instantiate an abstract class (or subclass) with unimplemented methods.
    – Jephron
    Nov 15, 2019 at 19:24
  • 1
    And when you raise NotImplementedError you can pass a string message to tell more detailed information to the caller about why the error is being thrown.
    – DtechNet
    Jan 15, 2020 at 17:40
  • 3
    Instead of pass or an elipsis (...) it's better to declare at least a docstring for the abstract method. pylint told me that.
    – sausix
    Dec 12, 2020 at 12:29
  • Thanks sausix, it's a good point. I'll include it into the answer Dec 13, 2020 at 11:42
11

I've modified just a bit @James answer, so that all those decorators do not take so much place. If you had multiple such abstract properties to define, this is handy:

from abc import ABC, abstractmethod

def abstractproperty(func):
   return property(classmethod(abstractmethod(func)))

class Base(ABC):

    @abstractproperty
    def CONSTANT(cls): ...

    def print_constant(self):
        print(type(self).CONSTANT)


class Derived(Base):
    CONSTANT = 42

class BadDerived(Base):
    BAD_CONSTANT = 42

Derived()       # -> Fine
BadDerived()    # -> Error

2
  • The one line abstractproperty looks very useful, but it gives me an error on Python 3.10 and Pylance: Argument of type "classmethod[Unknown]" cannot be assigned to parameter "fget" of type "((Any) -> Any) | None" in function "init" Type "classmethod[Unknown]" cannot be assigned to type "((Any) -> Any) | None" Type "classmethod[Unknown]" cannot be assigned to type "(Any) -> Any" Type cannot be assigned to type "None" Feb 3, 2023 at 8:43
  • @JanDolejsi I have tested it on the online python 3.11 tool and it works. Maybe some python 3.10 bug?
    – dankal444
    Feb 3, 2023 at 11:51
7

Python3.6 implementation might looks like this:

In [20]: class X:
    ...:     def __init_subclass__(cls):
    ...:         if not hasattr(cls, 'required'):
    ...:             raise NotImplementedError

In [21]: class Y(X):
    ...:     required = 5
    ...:     

In [22]: Y()
Out[22]: <__main__.Y at 0x7f08408c9a20>
1
  • Not sure this will raise a type error though if your subclasses don't implement the attribute, in case you're using type checking from an IDE
    – Allen Wang
    Jun 8, 2020 at 21:43
5

Your base class could implement a __new__ method that check for class attribute:

class Controller(object):
    def __new__(cls, *args, **kargs):
        if not hasattr(cls,'path'): 
            raise NotImplementedError("'Controller' subclasses should have a 'path' attribute")
        return object.__new__(cls)

class C1(Controller):
    path = 42

class C2(Controller):
    pass


c1 = C1() 
# ok

c2 = C2()  
# NotImplementedError: 'Controller' subclasses should have a 'path' attribute

This way the error raise at instantiation

1
  • 1
    interestingly, this doesn't work if you need to use def __init__(self).
    – szeitlin
    Oct 6, 2015 at 20:30
1
class AbstractStuff:
    @property
    @abc.abstractmethod
    def some_property(self):
        pass

As of 3.3 abc.abstractproperty is deprecated, I think.

0

Bastien Léonard's answer mentions the abstract base class module and Brendan Abel's answer deals with non-implemented attributes raising errors. To ensure that the class is not implemented outside of the module, you could prefix the base name with an underscore which denotes it as private to the module (i.e. it is not imported).

i.e.

class _Controller(object):
    path = '' # There are better ways to declare attributes - see other answers

class MyController(_Controller):
    path = '/Home'
3
  • 1
    is it possible to raise some error if the subclass does not redefine the attribute? It would be easy for methods, but how about attributes?
    – Mario F
    Apr 29, 2010 at 10:08
  • Wouldn't it be better to leave out the path declaration in _Controller class? Duck Typing wouldn't take effect if there is already a (invalid) value. Otherwise at some point, where I need the path field to be defined, there would be no error because there is already a value.
    – deamon
    Apr 29, 2010 at 10:25
  • @Mario - yes, Brendan Abel's answer gives a good way to do this
    – Brendan
    Apr 29, 2010 at 13:07
-1

Have a look at the abc (Abtract Base Class) module: http://docs.python.org/library/abc.html

However, in my opinion the simplest and most common solution is to raise an exception when an instance of the base class is created, or when its property is accessed.

1
  • 16
    Please elaborate: How does abc module help in this context?
    – guettli
    Jun 16, 2016 at 14:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.