Abstracting try..finally with generators (instead of macros)
Andrew Dalke
adalke at mindspring.com
Sun Dec 15 20:42:25 EST 2002
Beni Cherniavsky wrote:
> True, I forgot about for's fallback to `__getitem__`.
"Fallback"? That's true now. I'm getting too old, aren't I. :)
> About the `sys` reference: I think that the class, being defined in the
> module (and referncing global variables) retains the module -- which in
> turn retains sys by having it imported -- so there shouldn't be a problem.
> Do I miss anything?
I .. don't think it keeps a reference to the module, only the name of
the module. Else in the pre-garbage collection days there would be
a lot of cycles. (I think?)
>>> spam = Spam()
>>> dir(spam)
['__doc__', '__module__']
>>> spam.__module__
'__main__'
>>> type(spam.__module__)
<type 'str'>
>>> spam.__class__
<class __main__.Spam at 0x8103114>
>>> dir(spam.__class__)
['__doc__', '__module__']
>>> spam.__class__.__module__
'__main__'
>>> type(spam.__class__.__module__)
<type 'str'>
>>>
The problem is that objects in each module need to be deallocated
during shutdown. How's that done?
I dug up Guido's essay on this from the 1.5 days
http://www.python.org/doc/essays/cleanup.html
Don't know how true it is now.
It has
> M5. Clear all remaining modules carefully, except __builtin__ and sys.
But it also has
> M2. The three standard I/O files (sys.stdin, sys.stdout and
> sys.stderr) are restored to their initial values (which are
> saved as sys.__stdin__, sys.__stdout__ and sys.__stderr__,
> respectively, when the interpreter starts). If any of the
> initial values is unavailable, the corresponding object is
> set to None. [New.]
So there's no need to worry - 'sys' isn't cleared until the
very end.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list