if/elif chain with assignment expressions

Fernando Perez fperez528 at yahoo.com
Mon Jul 12 00:46:07 CEST 2004


Paul Rubin wrote:

> steve holden <sholden at holdenweb.com> writes:
>> Indeed, you  might think so. But this topic has been discussed
>> exhaustively (which isn't to say you aren't about to hear a lot more
>> about the subject) in this group, and the assignment operation is
>> *not* an operator for specific reasons the FAQ attempts to make clear
>> in
>>
http://www.python.org/doc/faq/general.html#why-can-t-i-use-an-assignment-in-an-expression
> 
> Yeah, I've seen those discussions before.  The if/elif/elif example was
> something I hadn't seen in those discussions, and it came up in some
> code I was writing yesterday, so I posted about it.

And your example is an excellent argument, IMHO, of why the current situation is
bad.  While the usage cases in the faq have easy alternatives (iterators), your
example does not, and you do end up forced to write a bunch of unnecessarily
verbose code.  In general I applaud python's efforts for being clear, readable,
and not error-prone.  But this is one of those cases where leaving the training
wheels on causes genuine grief (I have in the past many times run into the
example you mentioned, and have cursed it silently).

I very much doubt this will change in the language, but we can always hope :)

Cheers,

f



More information about the Python-list mailing list