if <assignment>:
Carl Banks
imbosol at vt.edu
Tue Nov 26 05:45:16 EST 2002
Duncan Booth wrote:
> 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.
Other people often clamor for assignment if here, because the way you
did is an unnatural and circumlocative way to do something most
people, myself included, want to do as a sequential piece of code.
I don't want an assignment if because I have a great distaste for it,
so I usually use other workarounds whenever I want to do something
like this. But I don't blame anyone who doesn't want to break up
their code the way you did, making it less straightforward in the
process.
--
CARL BANKS
More information about the Python-list
mailing list