What c.l.py's opinions about Soft Exception?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Mar 11 13:50:36 EDT 2008


On Tue, 11 Mar 2008 03:14:54 -0700, Lie wrote:

>> Regarding the number of callbacks: you can as well pass an object that
>> has several methods to call.
> 
> If you passed an object that has several methods to call (using tuple or
> list) and you want to handle several softexceptions and ignore some
> others, you must still pass an empty methods to the one you want to
> ignore, cluttering the caller's code by significant degree:
> 
>     def somefunc(a, b, callback = (DO_NOTHING, DO_NOTHING, DO_NOTHING,
> DO_NOTHING)):
>         if a == 0: raise callback(0)
>         try:
>             a += b
>         except ZeroDivisionError:
>             raise callback(1)
>         if a <= 0: raise callback(2)
>         raise callback(3)
>         return a
> 
>     somefunc(a, b, (callbackzero, DO_NOTHING, callbacktwo,
> DO_NOTHING))


Yes, you are right that this is a bad idea. But it is a bad idea 
regardless of whether you use callbacks or SoftExceptions.

In your example above, you seem to have accidentally written "raise 
callback" when you (probably) meant to just call the callback. That's 
significant because it shows that replacing the callback is still just as 
cluttered, and still puts a large burden on somefunc() to perform every 
test that callers might want to perform.

This is a bad idea because you are tightly coupling somefunc() to the 
specific needs of some arbitrary callers. You should aim to have loose 
coupling between functions, not tight. Tight coupling should be avoided, 
not encouraged.

In most circumstances, the right place to put the various tests is in the 
caller, not in somefunc().



-- 
Steven



More information about the Python-list mailing list