Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Requiring a Python class property to be explicitly set in its subclasses
# Base class
In [1]: class Foo(object):
...: @property
...: def bar(self):
...: raise NotImplementedError
...:
# Subclass 1, with property explicitly set
In [2]: class Foof(Foo):
...: bar = "taco"
...:
# Subclass 2, without property explicitly set
In [3]: class Foofer(Foo):
...: pass
...:
# Subclass 1 works as intended
In [4]: f = Foof()
In [5]: f.bar
Out[5]: 'taco'
# Subclass 2 borks
In [6]: f = Foofer()
In [7]: f.bar
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-7-9980d487a7ee> in <module>()
----> 1 f.bar
<ipython-input-1-511cebe5ae6b> in bar(self)
2 @property
3 def bar(self):
----> 4 raise NotImplementedError
5
NotImplementedError:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.