[Python-ideas] Anaphoric if

Ron Adam rrr at ronadam.com
Sat Apr 24 19:13:51 CEST 2010



On 04/23/2010 07:23 PM, Jess Austin wrote:

 > for the sake of completeness: would the following:
 >
 > if not foo() as x:
 >      ... x
 >
 > mean this:
 >
 > x = foo()
 > if not x:
 >      ... x


I would think it would defined as

      if expression as name:
          block using name

Which would be...

     x = not foo()
     if x:
         ... x

So it could be tricky to get the correct behavior unless (expression as 
name) becomes a valid expression in it self, but then it wouldn't be 
limited to the if block, so you might as well just use (expression = name).

Another issue would be, what if x already existed? would it just write over 
it, or raise a NameError. If it wrote over it, would it be restored after 
the block to it's previous value?  All of this adds unneeded complexity to 
the language without adding any real benefits.  Yes it reduces the line 
count by one, but what meaningful use case is enabled with this that we 
can't already do?

Ron





More information about the Python-ideas mailing list