What's the address for?

Chris Angelico rosuav at gmail.com
Tue Feb 19 13:32:09 EST 2019


On Wed, Feb 20, 2019 at 5:28 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2019-02-19, Chris Angelico <rosuav at gmail.com> wrote:
> > On Wed, Feb 20, 2019 at 3:23 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
> >
> >> Those object _may_ each have different identies.  Is it required that
> >> they do?  If an assignment is done on name referring to an object with
> >> no other references, would it be allowed that the "old" object is
> >> destroyed and its ID reused for a new object being created by the RHS
> >> of the assignment?
> >>
> >> Admittedly, that would be a rather odd way to do things.  One would
> >> expect that first the RHS is evaluated (perhaps creating a new
> >> object), then the name is rebound.
> >
> > Correct, the RHS is evaluated first. So the old object is still
> > around (important for statements like "x = x.lower()").
>
> Not doing the unbinding until after the RHS evaluation is also
> important for preventing surprises due to exceptions raised while the
> RHS is evaluated.  One would reasonably expect that if that happens
> the original binding of the name on the LHS is unchanged.
>
> Dunno if that's actually specified anywhere...
>

It is, yes:

https://docs.python.org/3/reference/expressions.html#evaluation-order

ChrisA


More information about the Python-list mailing list