Как доступ к элементам класса по индексам?
Как сделать доступ к элементам класса Point в классе Vector, я что-то упускаю. Пишет ошибку:
Traceback (most recent call last):
File "", line 39, in __getitem__
TypeError: 'Point' object is not subscriptable
Код:
class Point:
def __init__(self, x, y):
self.__x = None
self.__y = None
self.x = x
self.y = y
@property
def x(self):
return self.__x
@x.setter
def x(self, x):
if (type(x) == int) or (type(x) == float):
self.__x = x
@property
def y(self):
return self.__y
@y.setter
def y(self, y):
if (type(y) == int) or (type(y) == float):
self.__y = y
class Vector:
def __init__(self, coordinates: Point):
self.coordinates = coordinates
def __setitem__(self, index, value):
self.coordinates[index] = value
def __getitem__(self, index):
return self.coordinates[index]
Источник: Stack Overflow на русском