Why don't people like lisp?
Brian Kelley
bkelley at wi.mit.edu
Thu Oct 23 14:54:49 EDT 2003
Tayss wrote:
> http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=3F1F5297.15D768EF%40alcyone.com
> Erik Max Francis explains that expecting the system to close files
> leads to brittle code. It's not safe or guaranteed.
>
> After learning Python, people write this bug for months, until they
> see some article or usenet post with the try/finally idiom.
Some more than most. Interestingly, I write proxies that close
resources on failure but tend to let files do what they want.
> Now, I love Python, but this really is a case where lots of people
> write lots of potentially hard-to-reproduce bugs because the language
> suddenly stopped holding their hands. This is where it really hurts
> Python to make the tradeoff against having macros. The tradeoff may
> be worth it, but ouch!
I choose a bad example on abusing the C-implementation. The main thrust
of my argument is that you don't need macros in this case, i.e. there
can be situations with very little tradeoff.
class SafeFileWrapper:
def __init__(self, f):
self.f = f
def write(self, data):
try: self.f.write(data)
except:
self.f.close()
self.f = None
raise
def close():
if self.f:
self.f.close()
...
Now the usage is:
f = SafeFileWrapper(open(...))
print >> f, "A couple of lines"
f.close()
So now I just grep through my code for open and replace it with
SafeFileWrapper(open...) and all is well again.
I still have to explicitly close the file though when I am done with it,
unless I don't care if it is open through the application run. But at
least I am guaranteed that the file is closed either when I tell it to
or on an error.
Brian Kelley
More information about the Python-list
mailing list