How is the execution order of 'x = z'? (also: Python FAQ 4.88 )

Ames Andreas (MPA/DF) Andreas.Ames at
Wed Jul 23 10:31:05 CEST 2003


"Ulrich Petri" <ulope at> writes:

> 1. its not y's reference count that gets (possibly) decremented but
> the-object-y-points-to

Thanks for the clarification, that's what I wanted to say (but failed
to say).

> 2. in this example noting will be decremented since y never goes out of
> scope. Just by assining y to x you dont ~invalidate~ y.

Obviously my sample was somehow ambigous.  The assignment I cared
about was 'x = z' (like in the subject).  I wanted to make the sample
clear by the other assignment.  Sorry!

Below is a more selfcontained sample (assume that x, y and z are not
bound before the code is executed):

>>> class Foo:
...     def __del__(self):
...             print '__del__(%#x)' % id(self)
>>> y = Foo()
>>> print '%#x' % id(y)
>>> z = 0
>>> print '%#x' % id(z)
>>> x = y
>>> print '%#x' % id(x)
>>> del y
>>> print '%#x' % id(x)
>>> x = z
>>> print '%#x' % id(x)

The assignment 'x = z' calls Foo.__del__.  What I wanted to know is:

Is Foo.__del__ called *before* x refers to an integer object of value
0 or *afterwards*.  In other words, does x refer to a possibly
(partially) invalid object while Foo.__del__ is executed, or what is
the Python FAQ 4.88 referring to (within the final paragraph which
begins with 'Note:', dealing with atomicity of operations replacing



More information about the Python-list mailing list