Arent these snippets equivalent?

Roy Smith roy at panix.com
Wed Jan 23 23:47:55 CET 2013


In article <mailman.923.1358979203.2939.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> Personally, I'd much rather have a 'while' condition that does
> assignment, but that's something Python's unlikely ever to do.
> There've been various proposals to make that possible, but ultimately
> the only way to make that work is for assignment to be an expression,
> which is right up there alongside braces defining blocks.

while getchar() as c:
   putchar(c)

That would give people (including me) the use case they're after most of 
the time (call a function, assign the return value, and test it).  It's 
way less klunky than:

while True:
   c = getchar()
   if c:
      break
   putchar()

It wouldn't require assignment as an expression, or braces, or any new 
keywords.  It would also be quite analogous to

try:
   blah()
except BogusThing as ex:
   whatever()

in both cases, the effect is "perform some action, grab a value which 
resulted from that, and if it passes some test, make it available in the 
next block bound to a name".



More information about the Python-list mailing list