C-like assignment expression?
Diez B. Roggisch
deets at nospam.web.de
Wed May 21 09:39:10 EDT 2008
cokofreedom at gmail.com wrote:
> On May 21, 3:12 pm, "boblat... at googlemail.com"
> <boblat... at googlemail.com> wrote:
>> On May 21, 1:47 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
>>
>> > Although that solution is pretty, it is not the canonical solution
>> > because it doesn't cover the important case of "if" bodies needing to
>> > access common variables in the enclosing scope. (This will be easier
>> > in Python 3 with 'nonlocal', though.) The snippet posted by Diez is
>> > IMHO closer to a canonical solution to this FAQ.
>>
>> Hello everybody,
>>
>> thanks for the various answers. I'm actually pretty puzzled because I
>> expected to see some obvious solution that I just hadn't found before.
>> In general I find Python more elegant and syntactically richer than C
>> (that's where I come from), so I didn't expect the solutions to be a
>> lot more verbose and/or ugly (no offense) than the original idea which
>> would have worked if Python's assignment statement would double as
>> expression, as in C.
>>
>> Thanks again,
>> robert
>>
>> PS: Since I'm testing only three REs, and I only need the match
>> results from one of them, I just re-evaluate that one.
>
> Is it really a lot to change to have it
>
> if my_re1.match(line):
> match = my_re1.match(line)
> elseif my_re2.match(line):
> match = my_re2.match(line)
> elseif my_re3.match(line):
> match = my_re3.match(line)
>
> ?
>
> That reads clearly to me...
And wastes time. regular expressions can become expensive to match - doing
it twice might be hurtful.
Diez
More information about the Python-list
mailing list