On Mon, 03 Dec 2012 16:24:50 +1100, Chris Angelico wrote:
> On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano
> <[email protected]> wrote:
>> Consider this piece of legal Python code:
>>
>> Err = None
>> if condition(x) > 100:
>> Err = OneException
>> elif another_condition(x):
>> Err = AnotherException
>> try:
>> spam(a, b, c)
>> except Err:
>> recover()
>
> Legal it may be, but are there times when you actually _need_ this level
> of dynamism? It strikes me as a pretty weird way of going about things.
>
> I agree with the point you're making, but this feels like a contrived
> example, and I'm curious as to whether it can be uncontrived.
Yeah, in hindsight it was a pretty crappy example. But this sort of
dynamism really is useful:
def testRaises(exc, func, *args):
try:
result = func(*args)
except exc:
return
raise AssertionError("expected exception but didn't get one")
def wrap(func, exc, default=None):
@functools.wraps(func)
def inner(*args):
try:
return func(*args)
except exc:
return default
return inner
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list