while expression feature proposal

Cameron Simpson cs at zip.com.au
Fri Oct 26 19:56:01 EDT 2012


On 26Oct2012 19:41, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
| On Fri, Oct 26, 2012 at 6:03 PM, Cameron Simpson <cs at zip.com.au> wrote:
| > Any doco would need to make it clear that no order of operation is
| > implied, so that this:
| >
| >   x = 1
| >   y = (2 as x) + x
| >
| > does not have a defined answer; might be 2, might be 3. Just like any
| > other function call with side effects.
| 
| But function calls with side effects _do_ have a defined order of
| evaluation. Left to right.
| And the answer should be 4.
| http://docs.python.org/reference/expressions.html#evaluation-order

No. Separate _expressions_ are evaluated left to right.

So this:

  f(1), f(2)

calls "f(1)" first, then "f(2)". But this:

  f(1) + f(2)

need not do so. Counter-documentation welcomed, but the doco you cite
does not define an order for the second example above.

| 
| >>> def set_(d, k, v):
| ...     d[k] = v
| ...     return v
| ...
| >>> d = {}
| >>> set_(d, 'x', 1)
| 1
| >>> set_(d, 'y', set_(d, 'x', 2) + d['x'])
| 4

That may just be a coincidence of implementation - there's no special
reason to change the evaluation order form the lexical order there, but
expression optimisers should have a free hand generally.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

Acceptance Testing: Dropping your mods straight into the production
                    environment to see if the users will accept them.



More information about the Python-list mailing list