I can't get my head around why do name of getter/setter methods have to have the same name fo the property.
I've tried reading Aaron Hall's answer here, but I still couldn't find an explanation (or missed it) about why we have to use the respective names.
class Car(object):
def __init__(self, color='blue'):
self.color = color
self.current_model = 'Opel'
@property
def model(self, new_model):
return self.current_model
@model.setter
def model(self, new_model):
if new_model == 'Audi':
raise ValueError ('NO AUDI ALLOWED')
else:
self.current_model = new_model
# model = model.setter(models) but doing this works
@model.getter
def model(self):
return self.current_model
Edit: What I found confusing is that if I rename my method as:
@model.setter
def model_new(self, new_model):
if new_model == 'Audi':
raise ValueError ('NO AUDI ALLOWED')
else:
self.current_model = new_model
And I try running:
audi = Car()
audi.model = 'BMW' # will get AttributeError: can't set attribute
I am expecting that this does not work because getter, setter, and deleter methods create a copy of the property, and changing the name of the method is like modifying attributes of a different property. It is like doing: models = model.setter(models).
But i am not sure if I get this correctly