[Python-ideas] Where-statement (Proposal for function expressions)
David Stanek
dstanek at dstanek.com
Fri Jul 17 13:50:55 CEST 2009
On Fri, Jul 17, 2009 at 4:20 AM, Daniel
Stutzbach<daniel at stutzbachenterprises.com> wrote:
> On Thu, Jul 16, 2009 at 9:02 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>>
>> I would expect x to be bound in the current scope,
>> not the where-block scope. But I would expect
>>
>> x[i] = 5 where:
>> x = y
>> i = 7
>>
>> to be equivalent to
>>
>> y[7] = 5
>
> How about if attempts to re-bind variables in outer scopes should throw an
> exception? Much like they already do in this example:
>
>>>> x = 5
>>>> def foo():
> ... blah = x
> ... x = 6
> ...
>>>> foo()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in foo
> UnboundLocalError: local variable 'x' referenced before assignment
>
> (rebinding is allowed if "global" or "nonlocal" is explicitly used, of
> course)
>
This thread is a little confusing to me. I am failing to see how the where
block makes anything clearer. The fact that a new scope is created and names
are available during a singe line of execution scares me.
I also worry that this will encourage code duplication. Any substantial logic
should be rolling into a function not into a where block.
--
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
More information about the Python-ideas
mailing list