Python Subscript not Working – Builtin Function or Method ‘object’ is not subscribable. This error is generated when you try to access an object’s attribute through a sequence index. But the object does not have a corresponding sequence of attributes. The best way to fix this error is to add a subscription function. The code below will index the object’s attribute according to its position in the sequence: def __getitem__(self, key): if key == 0: return self._x Elif key == – len (self) + IDX : return self._y else : raise IndexError(“Index out of range”) ‌‍‍@property def x(self):

ball, binary, computer data @ Pixabay

“Returns ‘_x’.” return _internal_object[‘xx] @property def y(self): “Returns ‘_y’.” return _internal_object[‘y’] Experienced Python programmers know that when you try and access an object’s attribute through a sequence index, but the object does not have a corresponding sequence of attributes, it will generate an error. The best way to fix this error is to add a subscription function in order to index the attribute according to its position in the sequence.

The code below will do just that: def __getitem__(self, key): if key == 0: return self._x elif key == – len (self) + idx : return self._y else : raise IndexError(“Index out of range”) ‌‍‍@property def x(self): “”Returns ‘_x’.”” return _internal_object[‘x’] @property def y(self): “”Returns ‘_y’.”

LEAVE A REPLY

Please enter your comment!
Please enter your name here