How best to write this if-else?

Fredrik Lundh fredrik at pythonware.com
Mon Apr 23 10:50:32 EDT 2001


Harry George wrote:
> It would be helpful if the "re" package would support this idiom in
> future releases.

the "regex" module did this -- but that approach isn't thread-safe,
so that approach was dropped when Python switched to "re".

on the other hand,

    for match in (e1.match, e2.match, e3.match):
        m = match(line)
        if m:
            text = m.group(1)
            break
    else:
        print "no match"

isn't that bad...

Cheers /F





More information about the Python-list mailing list