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

Duncan Booth duncan.booth at invalid.invalid
Fri Jun 29 12:40:55 EDT 2007


Douglas Alan <doug at alum.mit.edu> wrote:

>> Is one of your preconditions that no one will ever handle an
>> exception raised by your code or by their own code when it is
>> invoked by yours?
> 
> A precondition of much of my Python code is that callers won't
> squirrel away large numbers of tracebacks for long periods of time.  I
> can live with that.  Another precondition of much of my code is that
> the caller doesn't assume that it is thread-safe.  Another
> precondition is that the caller doesn't assume that it is likely to
> meet real-time constraints.  Another precondition is that the caller
> doesn't need my functions to promise not to generate any garbage that
> might call the GC to invoked.

None of that is relevant.

Have you ever seen any code looking roughly like this?

def mainloop():
   while somecondition:
      try:
          dosomestuff()
      except SomeExceptions:
          handletheexception()

Now, imagine somewhere deep inside dosomestuff an exception is raised while 
you have a file open and the exception is handled in mainloop. If the loop 
then continues with a fresh call to dosomestuff the traceback object will 
continue to exist until the next exception is thrown or until mainloop 
returns.

There is no 'squirrelling away' needed here. The point is that it is easy 
to write code which accidentally holds onto tracebacks. It is not 
reasonable to expect the caller to analyse all situations where the 
traceback object could continue to exist.



More information about the Python-list mailing list