[Python-ideas] If branch merging
wolfram.hinderer at googlemail.com
Tue Jun 9 20:54:00 CEST 2015
Am 08.06.2015 um 23:26 schrieb Nick Coghlan:
> On 9 June 2015 at 01:24, Wolfram Hinderer
> <wolfram.hinderer at googlemail.com> wrote:
>> Am 08.06.2015 um 14:38 schrieb Nick Coghlan:
>>> On 8 June 2015 at 22:12, Steven D'Aprano <steve at pearwood.info> wrote:
>>> [In relation to named subexpressions leaking to the surrounding
>>> namespace by default]
>>>>> What does "x[(a.b as b)] = b" mean
>>>> surely it simply means the same as:
>>>> b = a.b
>>>> x[b] = b
>>> Right, but it reveals the execution order jumping around in a way that
>>> is less obvious in the absence of side effects.
>> I'm lost. The evaluation order of today (right hand side first)
>> would make "x[(a.b as b)] = b" mean
>> x[a.b] = b
>> b = a.b
>> (assuming looking up a.b has no side effects).
> That assumption that the LHS evaluation has no side effects is the one
> that gets revealed by named subexpressions:
>>>> def subscript():
> ... print("Subscript called")
> ... return 0
>>>> def value():
> ... print("Value called")
> ... return 42
>>>> def target():
> ... print("Target called")
> ... return [None]
>>>> target()[subscript()] = value()
> Value called
> Target called
> Subscript called
Hm, that's my point, isn't it?
The evaluation of subscript() happens after the evaluation of value().
The object that the RHS evaluates to (i.e. value()) is determined before
subscript() is evaluated. Sideeffects of subscript() may mutate this
object, but can't change *which* object is assigned.
x[(a.b as b)] = b
b = a.b
x[b] = b
then the evaluation of the LHS *does* change which object is assigned.
That's why I asked for clarification.
(I mentioned the thing about a.b not having side effects only because in
x[a.b] = b
b = a.b
a.b is called twice, so it's no exact representation of what is going on either. But it's a lot closer, at least the right object is assigned ;-) )
More information about the Python-ideas