Answer the question
In order to leave comments, you need to log in
Can a Python class mimic the behavior of an array?
Is it possible in Python 3.x to set a class to refer to its instance as an array:
class A():
...
a = A()
a[1] # нужно чтобы можно было вот так обращаться к экземпляру класса
Answer the question
In order to leave comments, you need to log in
Yes, of course it can. Define the __iter__ and __getitem__ methods . The first is to be able to use in a loop like for i in a, the second to access a[i]. In the same place there is a description of the __setitem__ method and a lot of other interesting things.
Inherit from the standard list and write your own blackjacks and methods.
I did not find the necessary magic methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question