On Jun 29, 3:42 pm, Douglas Alan <[EMAIL PROTECTED]> wrote: > Michele Simionato <[EMAIL PROTECTED]> writes: > >> I've written plenty of Python code that relied on destructors to > >> deallocate resources, and the code always worked. > > You have been lucky: > > No I haven't been lucky -- I just know what I'm doing. > > > > > $ cat deallocating.py > > import logging > > > class C(object): > > def __init__(self): > > logging.warn('Allocating resource ...') > > > def __del__(self): > > logging.warn('De-allocating resource ...') > > print 'THIS IS NEVER REACHED!' > > > if __name__ == '__main__': > > c = C() > > > $ python deallocating.py > > WARNING:root:Allocating resource ... > > Exception exceptions.AttributeError: "'NoneType' object has no > > attribute 'warn'" in <bound method C.__del__ of <__main__.C object at > > 0xb7b9436c>> ignored > > Right. So? I understand this issue completely and I code > accordingly.
What does it mean you 'code accordingly'? IMO the only clean way out of this issue is to NOT rely on the garbage collector and to manage resource deallocation explicitely, not implicitely. Actually I wrote a recipe to help with this a couple of months ago and this discussion prompted me to publish it: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523007 But how would you solve the issue using destructors only? I am just curious, I would be happy if there was a simple and *reliable* solution, but I sort of doubt it. Hoping to be proven wrong, Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list