How is the execution order of 'x = z'? (also: Python FAQ 4.88 )
Ames Andreas (MPA/DF)
Andreas.Ames at tenovis.com
Wed Jul 23 04:31:05 EDT 2003
Hello,
"Ulrich Petri" <ulope at gmx.de> 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)
0x8152e7c
>>> z = 0
>>> print '%#x' % id(z)
0x80cbf70
>>> x = y
>>> print '%#x' % id(x)
0x8152e7c
>>> del y
>>> print '%#x' % id(x)
0x8152e7c
>>> x = z
__del__(0x8152e7c)
>>> print '%#x' % id(x)
0x80cbf70
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
objects)?
TIA,
andreas
More information about the Python-list
mailing list