[Python-ideas] if with as

Larry Hastings larry at hastings.org
Mon Mar 5 08:52:07 CET 2007


Talin wrote:
> Armin Ronacher wrote:
>   
>>         if name_re.match(text, pos) as match:
>>     
> Personally, I like it - [...] it seems to me that we are, in fact, 
> establishing a general rule that:
>
> 	<keyword> <expression> as <variable>:
>
> ...is a common syntactical pattern in Python, meaning 'do something 
> special with expression, and then as a side effect, assign that 
> expression to the named variable for this block."
I like it too.  However: unlike "except x as e" and "with a as b", the 
indented blocks under if and else don't establish a new scope.  So it 
begs the question: what is the lifetime of the variable created by "if x 
as y" here?  If it magically goes away, even as you create new variables 
in the scope, that just seems a little too weird to me.  If it outlives 
the nested block under the if, that's weird too.

(Personally I'd prefer it if the blocks under if and else *did* 
establish a new scope, but I know that's never going to change.)

Cheers,

/larry/



More information about the Python-ideas mailing list