[Python-ideas] Where-statement (Proposal for function expressions)

Chris Perkins chrisperkins99 at gmail.com
Fri Jul 17 02:26:53 CEST 2009


On Thu, Jul 16, 2009 at 2:51 PM, Paul Moore<p.f.moore at gmail.com> wrote:
> 2009/7/16 Chris Perkins <chrisperkins99 at gmail.com>:
>>> You've also disallowed
>>>
>>> x[i] = 12 where:
>>>    i = some_complicated_expression()
>
> 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).

I don't follow this argument - in what way is that how functional
languages work?

I see that code as analogous to the following Clojure, for example:

(def x (into-array [1 2]))
(def i 0)
(aset x i (let [i 1] 99))

You wouldn't expect x to now be [1 99], rather than [99 2], would you?

> I'd argue that having this example use a previously defined value of i, when
>
> x = i where:
>    i = some_complicated_expression()
>
> uses the i defined in the where clause, is a disaster waiting to happen.

I don't really see the problem with it. If you told people that the
where block only applies to the right-hand-side of an assignment, and
that the left-hand-side is always evaluated outside the scope of the
where block, I doubt there would be much confusion.

> On another point that came up -
>
> i = 5
> print i # 5
> x[i] = 12 where:
>    global i
>    i = 3
> print i # 3
>
> seems to me to be so unexpected and non-obvious that it makes the
> whole construct suspect. 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.

Again, I don't think that's a big deal. One solution is to disallow
global statements (if we're already disallowing stuff like break and
return, why not). Another is just "don't do that".

> 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.

"Namespaces are one honking great idea -- let's do more of those!"

And everyone knows, you can't argue with the Zen. ;)


Chris Perkins



More information about the Python-ideas mailing list