I was reading this link about using getters in Python, quote:
The Pythonic way is to not use them. If you must have them then hide them behind a property.
That same page includes various examples, but my question is, if getters are not the Python way, how would I indicate to someone reading my code that a variable should be read-only after construction.
Suppose I have the following class:
class Car:
def __init__(self, ID, name, tire, engine):
self.ID = ID
self.name = name
self.tire = tire
self.engine = engine
def __str__(self):
return "ID: {0} Name:{1} Tire: {2} Engine: {3}".format(self.ID, self.name, self.tire, self.engine)
If I wanted to indicate to another developer that self.engine
is read-only after being set in the constructor
, how would I do so? In orders words, if clients violate the rule, and attempt to set it, it's there problem if the implementation of self.engine
changes(for example, from object of the class Engine
to Dictionary
).