Ifs and assignments

Roy Smith roy at panix.com
Thu Jan 2 23:14:47 EST 2014


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

> while (var = func())
> {
>     ....
> }
> 
> In Python, that gets a lot clunkier. The most popular way is to turn
> it into an infinite loop:
> 
> while True:
>     var = func()
>     if not var: break
>     ....

Or turn it into a generator:

def funcinator():
   while True:
      var = func()
      if var:
         yield var
      else:
         break

for var in funcinator():
   ....

That certainly makes your mainline code cleaner, but it's a lot of crud 
when all you really wanted to write was:

while func() as var:
   ....



More information about the Python-list mailing list