I have a number of string constants that I define a default for in a base class. These are essentially class rather than instance variables. Each class that inherits from the base can redefine these constants if need be, but they are guaranteed to have them present due to inheritance. The constants are only needed internally to the class
I can think of two ways of declaring these and was wondering which is most Pythonic.
Option 1:
class A:
_VAR1 = "some string"
_VAR2 = "another string"
def _instance_method(self):
print(self._VAR1, self._VAR2)
class B(A):
_VAR1 = "a different string"
Option 2:
class A:
@cached_property
def _var1(self):
return "some string"
@cached_property
def _var2(self):
return "another string"
class B(A):
@cached_property
def _var1(self):
return "a different string"
Is one of these a more “Python” way of doing things? Is there a third option I’m not considering?
SNS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6