What determines a variable as an array in Python?

2

I'm studying about queues in Python and I'm using the object deque of the module collections .

So, in Python I can access the values of an array by index simply by typing:

array = [1, 2, 3]
arra[0] # 1

However, the deque object also allows me to access the values of the queue through an index, with the same array notation.

See:

fila = deque()
fila.append(1)
fila.append(2)
fila.append(3)

fila[0] # 1

Only the array is of class list and fila is of class deque of module collections see below:

print(type(fila))
print(type(array))

Output:

  

<class 'collections.deque'>
<class 'list'>

And this left me with the following doubt.

Doubt

  • Type list is what determines the variable as array? If yes, how does an object of the type of another class (i.e. deque ) have the same behavior as an array?
asked by anonymous 04.09.2018 / 21:14

2 answers

5

In Python the type does not define anything.

What you define is the object contract with the function you will perform. For example, when you do obj[x] , regardless of what obj or x are, the Python interpreter will evaluate the code as a call to the __getitem__ method of obj . If it has this method, its code will work regardless of whether it is a list, a queue, a tuple, or any other type.

In this case, the deque class also implements the __getitem__ method, as well as list , tuple , str , and so on. So it works to fila[0] .

>>> import collections
>>> print(dir(collections.deque))
[..., '__getattribute__', '__getitem__', '__gt__', ...]

For example, you could implement any class with a very characteristic behavior:

class Tabuada:
    def __init__(self, valor):
        self.valor = valor

    def __getitem__(self, item):
        return item * self.valor

So you can create the table of 2, t = Tabuada(2) , and check their values:

>>> print(t[1])  # 2
>>> print(t[2])  # 4
>>> print(t[3])  # 6
>>> print(t[4])  # 8

And so on.

See that the Tabuada class has no relation to the lists, neither inherits nor uses, but by setting the __getitem__ method, you can access the indexes through notation with brackets.

    
04.09.2018 / 21:44
6

list is the name that Python gives to what is known as array . In fact the pure array as we know Python does not have. It is a structure of data that are sequenced and that each element is accessed at time O (1) and the addition or removal of data added at the tips can be made, but with cost O (N), although there may be some optimizations to minimize this in some cases, guaranteed even only the linear complexity, so although that allowed it can be very slow to add or remove items from the list.

An important point to note is that this array is always pointers to values, so you can put any data in it.

deque is another structure with different characteristics. The addition and removal at the tips is O (1) (this is not so true in all cases), therefore very fast, but accessing the elements randomly does not give quickly, access is O (N), although on average it can be O (N / 2). deque does not behave like an array and the question starts from a false premise. You can access by index, but with a very different appointment.

Documentation .

Doing the same operation does not mean that it's the same, different complexities make every difference and are the basis of computer optimizations. The way the data is organized internally and the algorithm used makes it impractical for a given operation. It is very common to use the wrong structure and the person does not understand or understand why something is slow.

Nothing prevents any structure from behaving identically to the list, although it probably does not make sense to have this, except for an abstraction that makes sense. In the standard library certainly does not have since it should only have more concrete mechanism required for various operations. The collections contained in the documentation above and the language patterns always have different characteristics, although all can be accessed by an index. Some are even abstractions to make it easier to use language types, probably because it was a mistake that types are special in language, almost all languages use these types as patterns in the library rather than making the compiler know how to handle them. p>     

04.09.2018 / 21:41