today someone tells me a very interesting idea about property.
When we use the property, we want to unable others' ability to modify our class directly.
When we consult the professor about how to use it, he smiles and says that it is enough to only set the get_method of the property.
def set_coord(self: "Points", coord: [float, ]) -> None:
"""Set coordinates for this point"""
if '_coord' in dir(self):
raise Exception('Cannot reset coords')
else:
self._coord = coord
def get_coord(self: 'Point') -> [float, ...]:
"""Get coordinates for this Point"""
return self._coord
coord = property(get_coord, set_coord, None, None)
this is the class example of how to using property.
it is true that we are unable to change the self.coord directly after initialization.
However, someone figures out a bad idea.
Notice it is perfect fine to change self._coord directly...which under property method, it is effective...
I think that maybe this is why programmers look awesome...complexing things on purpose.
i still don't understand how does the property method work, but i think you did a good job :)
ReplyDeleteme too;)
Delete