List Order of Initialization

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Oct 10 23:41:34 CEST 2008


SamFeltus <samfeltus at gmail.com> writes:

> When a list initializes, will it always evaluate in order starting at
> element 0 and finishing with the last element?
> 
> def f1(x):
>     return x + 2
> 
> def f2(x):
>     return x * 2
> 
> def f3(x):
>     return x * 3
> 
> the_list = [f1(7), f2(8), f3(4)]

The new list won't even *exist* until all the arguments to the
constructor are evaluated; its initialiser will receive values, not
expressions. So, you're not asking about the behaviour of a list
initialising; you're asking about the order of evaluating an
expression.

In the above expression, yes, the several items in the literal list
expression will be evaluated left to right.

-- 
 \      “Two rules to success in life: 1. Don't tell people everything |
  `\                                            you know.” —Sassan Tat |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list