Cascading ifs

Sick Monkey sickcodemonkey at gmail.com
Thu Apr 5 16:22:14 EDT 2007


If it is just the indentation that is bothering you, you could do this:

match = "test"
if match == "1":
    print "1"
elif match == "2":
    print "2"
elif match == "test":
    print "test"

On 4/2/07, Ernesto García García <titogarcia_nospamplease_ at gmail.com> 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.
>
> Thanks in advance and regards,
> Ernesto
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070405/0a17af8b/attachment.html>


More information about the Python-list mailing list