I managed to bypass the setter using the following code :
t = TrajetGPS()
# will call t.getPoints(), which will return the list by reference
# we can then modify it at will, bypassing the setter
points = t.points
points.append(PointGPS(0, 0, 0))
points.append(PointGPS(1, 1, 1))
print(t) # will show : Points : (0, 0, 0) (1, 1, 1)
The TrajetGPS()
is defined as a simple class containing a list of PointGPS
defined with points = property(getPoints, setPoints)
and PointGPS()
contains 3 coordinates (latitude, longitude, altitude). But all this doesn't matter here.
Is it normal I can bypass the setter without a warning ?
Python is designed to be beginner-friendly, but this doesn't seem very friendly as it can be very quick to insert malformed data into the list (without knowing !).
You are actually NOT "bypassing the setter" - the setter is invoked when your trying to set t.points
(ie: t.points = []
), not when you are mutating the list returned by the setter.
If you don't want client code to mutate the list, either return some immutable structure (which will make clear that it's not supposed to be changed) or at least return a copy of the list (which will then still be mutable, but the modification wont affect the original list - note that this might suprise your code users).
NB : in your code snippet there is this comment:
which will return the list by reference
If you hope to get anything done in Python, stop thinking in terms of some other language and understand how Python works instead. There's is nothing like "by reference" or "by value" in Python, only names and objects. Your getter doesn't "return the list by reference", it returns the list, period. I strongly suggest your read this reference article (yeah, lame pun, sorry) for more on Python's concepts of names and objects.