[Python-ideas] Where-statement (Proposal for function expressions)
Jan Kaliszewski
zuo at chopin.edu.pl
Thu Jul 16 22:08:46 CEST 2009
16-07-2009, 20:51 Paul Moore <p.f.moore at gmail.com> wrote:
>>> x[i] = 12 where:
>>> i = some_complicated_expression()
[snip]
> And yet the user's intent is obviously to use the i defined inside the
> where (that's how functional languages work, so it's hardly an
> unrealistic intent).
[snip]
> Yes, I know that the semantics, as given in
> terms of a rewrite, define what happens, but it seems to me that it
> goes counter to intuition.
>
> Thinking about it, the issue is that you're making where the *only*
> example in Python (that i can think of) of a non-defining construct
> which creates a new scope. Currently, scopes are created by the class
> and def statements. Both of these define objects (classes and
> functions, respectively). The where clause as you've stated it defines
> a scope, but no object. That doesn't make it ill-defined, but it does
> make it (very, in my view) non-intuitive.
Yeah, It is an issue :-/
I can imagine some solutions based on name overriding rules, e.g.:
* in the "target line" only those names are treated as part of nested
where-block scope, which are targets of assignment within the where-block
(as MRAB proposes, if I understood well), or
* in the "target line" only those names are treated as part of nested
where-block scope, which are not targets of assignment within the target
line.
But it seems to me obscure at first sight, and I don't know if
implementation wouldn't be problematic...
Maybe the solution is to make 'where' creating an object, somehow
similarly to the way the 'class' statement works? Some examples:
x[somename.i] = 12 where somename:
i = some_complicated_expression()
def variance(data): # for some definition of "variance"
return var.sqrt(var.sum_of_squares(data)
- var.square_of_sums(data)) where var:
def sqrt...
def sum_of_squares...
def square_of_sums...
foo(numbers.x, numbers.y) where numbers:
x = a * 42
y = b / 17
a = {}
a[fn.key] = bar(fn.f, fn.g) where fn:
key = ...
def f(x):
...
def g(y):
...
Another possibility is to allow using "anonymus name":
x[.index] = 12 where:
index = some_complicated_expression()
foo(.x, .y) where:
x = a * 42
y = b / 17
Cheers,
zuo
More information about the Python-ideas
mailing list