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

Douglas Alan doug at alum.mit.edu
Mon Jul 2 14:07:57 EDT 2007


Lenard Lindstrom <len-l at telus.net> writes:

>> You don't necessarily want a function that raises an exception to
>> deallocate all of its resources before raising the exception, since
>> you may want access to these resources for debugging, or what have
>> you.

> No problem:
>
> [...]
>
>  >>> class MyFile(file):
> 	def __exit__(self, exc_type, exc_val, exc_tb):
> 		if exc_type is not None:
> 			self.my_last_posn = self.tell()
> 		return file.__exit__(self, exc_type, exc_val, exc_tb)

I'm not sure I understand you here.  You're saying that I should have
the foresight to wrap all my file opens is a special class to
facilitate debugging?

If so, (1) I don't have that much foresight and don't want to have
to.  (2) I debug code that other people have written, and they often
have less foresight than me.  (3) It would make my code less clear to
ever file open wrapped in some special class.

Or are you suggesting that early in __main__.main(), when I wish to
debug something, I do something like:

   __builtins__.open = __builtins__.file = MyFile

?

I suppose that would work.  I'd still prefer to clear exceptions,
though, in those few cases in which a function has caught an exception
and isn't going to be returning soon and have the resources generally
kept alive in the traceback.  To me, that's the more elegant and
general solution.

|>oug



More information about the Python-list mailing list