Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuples readability : [0,0] vs (0,0)

Tags:

I'm using Python since some times and I am discovering the "pythonic" way to code. I am using a lot of tuples in my code, most of them are polar or Cartesian positions.

I found myself writing this :

window.set_pos([18,8]) 

instead of this :

window.set_pos((18,8)) 

to get rid of the double parenthesis I found hard to read.

It seems that python is automatically doing the type conversion from list to tuple, as my code works properly.

But is it a good way to code ? Do you have any presentation tip I could use to write readable code ?

Thank you in advance for your surely enlightening answers.

like image 225
Zoé Martin Avatar asked Jun 29 '12 12:06

Zoé Martin


People also ask

Do tuples start at 0 or 1?

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.

Are tuples more memory efficient?

Internally, tuples are stored a little more efficiently than lists, and also tuples can be accessed slightly faster.

Why tuples are more efficient?

Tuples are stored in a single block of memory. Tuples are immutable so, It doesn't require extra space to store new objects. Lists are allocated in two blocks: the fixed one with all the Python object information and a variable sized block for the data. It is the reason creating a tuple is faster than List.

Are tuples more efficient than lists?

Meanwhile, a tuple is immutable therefore its element count is fixed. So Python just needs to allocate enough memory to store the initial elements. As a result, the storage efficiency of a tuple is greater than a list.


2 Answers

I'd be careful deciding to eschew tuples in favor of lists everywhere. Have you ever used the dis module? Watch what Python is doing at the bytecode level when you make a list verses making a tuple:

>>> def f(): ...     x = [1,2,3,4,5,6,7] ...     return x ...  >>> def g(): ...     x = (1,2,3,4,5,6,7) ...     return x ...  >>> import dis >>> dis.dis(f)   2           0 LOAD_CONST               1 (1)               3 LOAD_CONST               2 (2)               6 LOAD_CONST               3 (3)               9 LOAD_CONST               4 (4)              12 LOAD_CONST               5 (5)              15 LOAD_CONST               6 (6)              18 LOAD_CONST               7 (7)              21 BUILD_LIST               7              24 STORE_FAST               0 (x)    3          27 LOAD_FAST                0 (x)              30 RETURN_VALUE      >>> >>>    >>> dis.dis(g)   2           0 LOAD_CONST               8 ((1, 2, 3, 4, 5, 6, 7))               3 STORE_FAST               0 (x)    3           6 LOAD_FAST                0 (x)               9 RETURN_VALUE    

Though it will probably never be an issue in a GUI application (as your example seems to be), for performance reasons you may want to be careful about doing it everywhere in your code.

like image 102
K. Brafford Avatar answered Oct 26 '22 23:10

K. Brafford


You say

It seems that python is automatically doing the type conversion from list to tuple

That's doubtful. Since lists and tuples are both sequence types, they both implement many of the same behaviors, and whatever GUI library you're using must not need any of the list-only behaviors.

It's probably fine to do this in many cases, but note that lists do take up a bit more space than tuples:

>>> sys.getsizeof((1, 2)) 72 >>> sys.getsizeof([1, 2]) 88 

And may be slower than tuples at some things:

>>> lst, tup = [1, 2], (1, 2) >>> def unpack(x): ...     a, b = x ...      >>> %timeit unpack(tup) 10000000 loops, best of 3: 163 ns per loop >>> %timeit unpack(lst) 10000000 loops, best of 3: 172 ns per loop 

These are very small differences that won't matter until you reach much larger scales -- as in billions of calls -- so the trade-off could be worth it.

Still, I don't see people do this very often. It seems like a nice readability trick, but it could have unexpected consequences in some circumstances. For example, if you pass a list you intend to use again later, then you have to be careful not to modify it inside the function. Finally, as J.F. Sebastian rightly points out, tuples and lists tend to mean slightly different things; using them in unconventional ways might negate the readability boost you seek.

like image 28
senderle Avatar answered Oct 27 '22 01:10

senderle