Return value of an assignment statement?

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu Feb 21 18:33:21 EST 2008


> What you can't do (that I really miss) is have a tree of assign-and-test
> expressions:
>
>         import re
>         pat = re.compile('some pattern')
>
>         if m = pat.match(some_string):
>             do_something(m)
>         else if m = pat.match(other_string):
>             do_other_thing(m)
>         else:
>             do_default_thing()

What you want is:

for astring, afunc in ((some_string, do_something), (other_string,
do_other_thing)):
    m = pat.match(astring)
    if m:
        afunc(m)
        break
else:
    do_default_thing()



More information about the Python-list mailing list