Im unteren Beispiel zum Festlegen von Koordinaten habe ich Getter und Setter ganz pythonisch als `property` umgesetzt. Der pythonische Setter schlägt aber mit folgender Fehlermeldung fehl:
Code: Alles auswählen
Traceback (most recent call last):
File "/home/ata/source/property-test.py", line 21, in <module>
element.coordinates((7, 2, 9))
TypeError: 'tuple' object is not callable
Code: Alles auswählen
class Element:
def __init__(self, x, y, z):
self._x = x
self._y = y
self._z = z
@property
def coordinates(self):
return self._x, self._y, self._z
@coordinates.setter
def coordinates(self, new_coordinate):
self._x, self._y, self._z = new_coordinate
def set_coordinates(self, new_coordinate):
self._x, self._y, self._z = new_coordinate
element = Element(10, 5, 8)
element.set_coordinates((5, 3, 8))
print(element.coordinates)
element.coordinates((7, 2, 9))
Atalanttore