python-dev summary, July 16-31

François Pinard pinard at iro.umontreal.ca
Thu Aug 9 18:45:31 EDT 2001


[Thomas Bellman]

> I wrote:

> > I would always write this as

> >    for x in [ 1, 2, 3 ]:
> >	pass

> I should probably explain this some more.  To me, a tuple isn't something
> that you iterate over.  [...]  The difference between lists and tuples
> isn't really that tuples are read-only, but in how they are used.  [...]

I understand all the nuances you make, and give you reason on them, as I
felt the same not long after I started to write Python code.

On the other hand, tuples are slightly more efficient than lists.  I think
(but am not fully sure) they are a bit more efficiently accessed, in that
there is no indirection from the tuple structure to the sequence of values,
and also, there is no overshoot allocation meant for possible growing.

So, even if we "think" lists, it is better to "write" tuples, despite they
do look more awkward given the context.  This has been a good incentive,
for me, at writing:

    for x in 1, 2, 3:
	pass

Because you do not commit yourself towards tuples or lists, your writing
stays clear, and Python merely does the most efficient choice internally.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list