Cascading ifs

Wojciech Muła wojciech_mula at poczta.null.onet.pl.invalid
Mon Apr 2 09:32:23 EDT 2007


Ernesto García García wrote:
> Hi experts,
> 
> How would you do this without the more and more indenting cascade of ifs?:
> 
> match = my_regex.search(line)
> if match:
>   doSomething(line)
> else:
>   match = my_regex2.search(line)
>   if match:
>     doSomething2(line)
>   else:
>     match = my_regex3.search(line)
>     if match:
>       doSomething3(line)
> 
> etc.

tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3, 
doSomething3)]
for regex, fun in tbl:
     match = regexp.match(line)
     if match:
        fun(line)
        break

w.



More information about the Python-list mailing list