What determines a variable as a Python array?

Asked

Viewed 679 times

3

I’m studying about lines in Python and I’m using the object deque module Collections.

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

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

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

Behold:

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

fila[0] # 1

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

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

Exit:

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

And this left me with the following doubt.

Doubt

  • The guy list is what determines the variable as array? If so, how does an object of the type of another class (i.e. deque) can have the same behavior as an array?
  • 1

    Define "same array behavior".

  • @Maniero the possibility of accessing the value directly through an index. Of course there must be other definitions, but do not know :)

  • cat notices that a tuple according to this definition (possibility to access through an Index) can also be an array, e.g.: (1,2,3)[0] == 1

  • @Miguel then tupla is an array too, this I didn’t even imagine xD

2 answers

6

List is the name Python gives to what is known as array. In fact the array pure as we know Python doesn’t. It is a data structure that are placed in sequence and that each element is accessed in O(1) time and the addition or removal of data added at the tips can be done, but at O(n) cost, although there may be some optimizations to minimize this in some cases, even guaranteed only linear complexity, so although allowed it can be quite slow to add or remove items from the list.

An important point to note that this array is always pointers to the values, this allows to put any data in it.

Deck 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, the access is O(n), although on average it may be O(n/2). deque does not behave like a array and the question starts from a false premise. You can access by the index, but with a very different commitment.

Documentation.

Doing the same operation does not mean it’s the same, different complexities make all the difference and is the basis of computation 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 one does not understand or realize why something is slow.

Nothing prevents any structure from behaving exactly the same as on the list, although it probably doesn’t make sense to have that, except for an abstraction that makes sense. In the standard library it certainly does not have since it must have only more concrete mechanism needed for various operations. The collections contained in the above documentation and the language patterns always have different characteristics, although all can be accessed by an index. Some are even abstractions to facilitate the use of language types, probably because it was a mistake for types to be special in the language, almost all languages use these types as standards in the library instead of making the compiler know how to deal with them.

6


In Python the type defines nothing.

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 is, the Python interpreter will evaluate the code as a call to the method __getitem__ 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 class deque also implements the method __getitem__, as well as the list, tuple, str, etc. That’s why it works 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 2 times table, 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 class Tabuada has no relation to the lists, neither inherits nor uses, but by defining the method __getitem__, you can access the indexes by means of bracketed notation.

  • 2

    It wouldn’t be interesting to define this behavior as Duck Typing in the reply, so that the staff know the term?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.