[Python-ideas] Where-statement (Proposal for function expressions)
MRAB
python at mrabarnett.plus.com
Thu Jul 16 20:20:27 CEST 2009
Jan Kaliszewski wrote:
> 16-07-2009, 17:37 Daniel Stutzbach <daniel at stutzbachenterprises.com> wrote:
>
>> On Thu, Jul 16, 2009 at 10:06 AM, Paul Moore <p.f.moore at gmail.com> wrote:
>>
>>> x[i] = 12 where:
>>> i = some_complicated_expression()
>>>
>>> Was that deliberate? If so, could you explain why now, so that it's on
>>> record for the legions of people who will ask after it's implemented?
>>> :-)
>>
>> Because only the right-hand side is in the where-block's scope.
>
> Yeah. But I believe it should be possible using global/nonlocal keywords,
> e.g.:
>
> x[i] = 12 where:
> global i
> i = some_complicated_expression()
>
> I think that many rules that apply for functions should also apply for
> where-blocks. (But obviously not all of them, e.g. return, yield and such
> stuff don't make sense)
>
> 16-07-2009, 18:16 Chris Perkins <chrisperkins99 at gmail.com> wrote:
>
>> Yes, I'm very interested to see what proportion of people find the
>> top-down style more readable.
>
> I think even for the same person it can strongly depend on particular
> application. It'll be good for some things, and useless for other.
> But it isn't a strong argument against the feature. IMHO it'll be
> useful in many situations.
>
> 15-07-2009, 14:49 Daniel Stutzbach <daniel at stutzbachenterprises.com> wrote:
>
>> How about the following as additional syntactic sugar for the common
>> one-function case?
>> x = blah(f) where def f(item):
>> body_of_f
>
> +1 from me also.
>
[Apologies for the word 'assignment' in what follows. :-)]
When defining a function, any name that's the target of an assignment
will default to being local; other names will be non-local.
In a 'where' construct the rule could be that any name that's the
target of an assignment before the 'where' will be non-local; other
names will default to local if they are the target of an assignment
within the 'where' construct, but non-local otherwise.
For example:
bar = "outside"
foo = bar where:
bar = "inside"
'foo' is non-local. There's an assignment to 'bar' with the 'where'
construct, so 'bar' is local in the statement and hides the non-local 'bar'.
Therefore 'foo' becomes bound to "inside".
More clearly:
bar = "outside"
local:
nonlocal foo
bar = "inside"
foo = bar
Another example:
x[i] = 12 where:
i = some_complicated_expression()
'x' is non-local because there's no assignment to 'x' within the 'where'
construct'. 'i' is local because there is an assignment to 'i' within
the 'where' construct'.
More clearly:
local:
i = some_complicated_expression()
x[i] = 12
More information about the Python-ideas
mailing list