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

Bjorn Pettersen BPettersen at NAREX.com
Tue Jul 22 22:28:15 CEST 2003


> From: Ames Andreas (MPA/DF) [mailto:Andreas.Ames at tenovis.com] 
> 
> Hello,
> 
> during execution of an assignment statement like
>
> x = y
> .
> .
> .
> x = z # <--
> 
> is y's reference count decremented (and therefore its __del__ possibly
> called) *before* x is bound to z (or is z bound to x, I don't know the
> correct wording) or *afterwards*?

I'm not sure exactly what you're asking, but "x = y", is similar to the
following C-pseudo-code:

 1 incref(*y);
 2 tmp = x
 3 x = y;
 4 decref(*tmp); // potentially calling __del__

[[if I'm reading ceval.c correctly... lines 2-4 being in SETLOCAL,
called from STORE_FAST, based on the following code:

>>> def f(a,b):
...    a = b
...
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_FAST                1 (b)
              3 STORE_FAST               0 (a)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE
]]

whether this should matter to you is dubious since the case you seem to
be worried about (y being deleted before z assigned to x) would only
matter if it caused z to have a dangling reference to y. However, if z
has a reference to y, its reference count can't go to zero, thus no
__del__ would ever be called.

-- bjorn





More information about the Python-list mailing list