C-like assignment expression?

Diez B. Roggisch deets at nospam.web.de
Wed May 21 09:34:56 EDT 2008


boblatest 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.

Well, it's a design-decision - and I'm pretty ok with it being a bit verbose
here - as it prevents a *great* deal of programming errors that would
otherwise happen from accidentally writing a = b where a == b was meant.

One could argue that regular expressions - which seem to be THE case where
it bugs people - should offer a standard way that essentially works as my
solution - by keeping state around, making series of tests easier.


Diez



More information about the Python-list mailing list