Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32247

Re: while expression feature proposal

Date 2012-10-27 10:56 +1100
From Cameron Simpson <cs@zip.com.au>
Subject Re: while expression feature proposal
References <CABicbJJkbK4fop0GSb2bofMu1_6X0vVgku4bo1O51b63S0A5ZQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2918.1351295765.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 26Oct2012 19:41, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
| On Fri, Oct 26, 2012 at 6:03 PM, Cameron Simpson <cs@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@zip.com.au>

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: while expression feature proposal Cameron Simpson <cs@zip.com.au> - 2012-10-27 10:56 +1100

csiph-web