catching exceptions from an except: block
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Mar 7 17:11:52 EST 2007
En Wed, 07 Mar 2007 18:48:18 -0300, Arnaud Delobelle
<arnodel at googlemail.com> escribió:
> for f in int, float, complex:
> try:
> return f(x)
> except ValueError:
> continue
> raise CantDoIt
>
> But if the three things I want to do are not callable objects but
> chunks of code this method is awkward because you have to create
> functions simply in order to be able to loop over them (this is whay I
> was talking about 'abusing loop constructs'). Besides I am not happy
> with the other two idioms I can think of.
Hmmm, functions are cheap - nobody is charging you $2 for each "def"
statement you write, I presume :)
A bit more serious, if those "chunks of code" are processing its input and
returning something that you further process... they *are* functions. If
you don't want them to be publicly available, use inner functions:
def xxxfactory(x):
def f1(x):
...
def f2(x):
...
def f3(x):
...
for f in f1,f2,f3:
try:
return f(x)
... same as above...
--
Gabriel Genellina
More information about the Python-list
mailing list