[Python-ideas] if with as
Armin Ronacher
armin.ronacher at active-4.com
Sat Mar 3 09:25:19 CET 2007
Hi all,
Many languages allow assignment expressions in if conditions. (Perl, PHP, Ruby,
C, etc..) I know that this was dismissed by guido because it can lead to
mistakes. And i can support that. However in some situations it might be
required because you have to nest if blocks and regular expressions for example::
while pos < text_length:
if match = name_re.match(text, pos):
pos = match.end()
do_something(match)
elif match = digit_re.match(text, pos):
pos = match.end()
do_something(match)
else:
pos += 1
Well. But that would require an assignment. Why not use the "as" keyword
introduced in python2.5 with the future import::
while pos < text_length:
if name_re.match(text, pos) as match:
pos = match.end()
do_something(match)
elif digit_re.match(text, pos) as match:
pos = match.end()
do_something(match)
else:
pos += 1
Advantages: Still no assignment expression, no additional keyword, simple to
understand.
Regards,
Armin
More information about the Python-ideas
mailing list