[Python-ideas] Anaphoric if

Guido van Rossum guido at python.org
Sun Apr 25 00:48:54 CEST 2010


On Sat, Apr 24, 2010 at 1:00 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Raymond Hettinger wrote:
>> Yes, this has come up before.
>> FWIW, I'm +1 on the idea,
>> especially if it can be also applied to while-loops.
>> It looks very readable
>
> Agreed, of the sundry embedded assignment proposals that come up
> semi-regularly, adding "as" clauses to if and while statements is the
> most viable (although possibly still not a good idea).

It is not a good idea. In particular it is a mistake to assume that
endowing if- and while-statements with special syntax is going to
solve anything -- the problem is more generally that *expressions*
can't have such effects (in Python) and there's nothing particularly
special about the top-level expression in an if-statement. It would be
just as likely to have a use case that required (using C syntax)
things like

  if ((p = foo() == NULL) || (q = p->next) != HEAD && q-> next != TAIL) ...

Also note that local variable assignment is super-fast so that there
is no need to worry about the speed of things like

  x = foo()
  if x:

compared to the hypothetical

  if foo() as x:

I expect Unladen Swallow would optimize the former anyway.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list