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

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jul 23 04:26:48 EDT 2003


"Ulrich Petri" <ulope at gmx.de> wrote in news:bfk2cd$ctqfr$1 at ID-
67890.news.uni-berlin.de:

>> 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*?
> 
> No.
> 1. its not y's reference count that gets (possibly) decremented but
> the-object-y-points-to
Correct

> 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.
> 
In this example the reference count of the-object-known-as-y is incremented 
by the assigment 'x=y' and decremented during the assignment 'x=z'.

In answer to the original question, is the reference count decremented 
before or after the assignment, the answer is 'after'. It has to be after 
the assignment has completed otherwise there would be a window, accessible 
during the call to __del__ where 'x' still referred to the original object, 
but the original object had a reference count of 0. It is usually fairly 
safe to assume that Python gets elementary things like reference counts 
right. (Unlike Microsoft whose reference counting on COM objects goes to 0 
more than once during their lifetime which can cause pain in free threaded 
programs.)


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list