Created
October 1, 2018 16:58
-
-
Save loisaidasam/f85d2676205c8f3ae80111cb5a0e5ba0 to your computer and use it in GitHub Desktop.
Requiring a Python class property to be explicitly set in its subclasses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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