destructors order not guaranteed?

Erno Kuusela erno-news at erno.iki.fi
Mon Oct 30 14:31:29 EST 2000


    
 |     if __name__ == '__main__':
 |        print 'Trying this in a function--'
 |        test()
 |        print 'Now trying it here--'
 |        a = foo(1)
 |        if 1:
 |            b = foo(2)

[...]

 |     Now trying it here--
 |     Constructing foo(1)
 |     Constructing foo(2)
 |     Destroying foo(2)
 |     Destroying foo(1)

 | As you can see the order of the destructors in not consistent and 
 | depends (on I guess the stack depth?). Clearly the second case is the 
 | expected behaviour but am not sure whats causing the wrong order in the 
 | case of a function. Is this a bug or am I missing something here? I can 
 | reproduce this behaviour with both 1.52 and 2.0

well, the second time around, you are executing in the module global
namespace and the varaible assignments become "permanent", ie your
objects live until the module object you are executing gets destroyed
(probably when the python interpreter exits), and not until the "if"
block.

i suspect the destruction of objects in the module-global namespace
happens in the order in which your variables happen to be extracted
from the global namespace dictionary (a hash table).

  -- erno



More information about the Python-list mailing list