Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Created October 1, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loisaidasam/f85d2676205c8f3ae80111cb5a0e5ba0 to your computer and use it in GitHub Desktop.
Save loisaidasam/f85d2676205c8f3ae80111cb5a0e5ba0 to your computer and use it in GitHub Desktop.
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