Ifs and assignments

Roy Smith roy at panix.com
Thu Jan 2 18:36:15 EST 2014


In article <mailman.4811.1388704420.18130.python-list at python.org>,
 John Allsup <pydev at allsup.co> wrote:

> if testFunc() as x:
> 	  do something with x

+1

The most common place I wish for an atomic "test and assign" is with 
regexes, as in your examples.  This would be so much nicer than what we 
have to do now:

    if re.match(string) as m:
        print m.group(0)

The analogy to

    except SomeError as ex:

is very clear.  They are both "Perform some action, capture the result, 
do some test on it, and then make that captured value available bound to 
a name".



More information about the Python-list mailing list