if <assignment>:

Duncan Booth duncan at rcp.co.uk
Tue Nov 26 05:04:08 EST 2002


Carl Banks <imbosol at vt.edu> wrote in news:aruf6o$bd7$3 at solaris.cc.vt.edu:

>> Now it's gonna be interesting to see if I ever run into a situation
>> where I'd feel most comfortable doing an if <assignment>:, knowing
>> that there are usually better ways of solving the problem. 
> 
> Testing a bunch of regular expressions on some input and taking action
> on the first match seems to do it for a lot of people.> 

If you have more than one or two patterns, then you should really be
thinking about a loop rather than a long list of if statements: 

PATTERNS = [
  (regex1, action1),
  (regex2, action2),
  # ...
]

for regex, action in PATTERNS:
    match = regex.match(string)
    if match:
        action(match)
        break

I don't see any need for assignment in an 'if' here.



More information about the Python-list mailing list