Python's "only one way to do it" philosophy isn't good?

Michele Simionato michele.simionato at gmail.com
Sat Jun 30 02:14:51 EDT 2007


On Jun 29, 3:42 pm, Douglas Alan <d... at alum.mit.edu> wrote:
> Michele Simionato <michele.simion... at gmail.com> 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




More information about the Python-list mailing list