[Python-Dev] Simple Switch statementZ

Guido van Rossum guido at python.org
Mon Jun 26 02:49:49 CEST 2006


On 6/25/06, Ka-Ping Yee <python-dev at zesty.ca> wrote:
> On Sun, 25 Jun 2006, Guido van Rossum wrote:
> > What do you think of Nick C's 'once'?
>
> It's a bit closer to the right meaning... but what about:
>
>     def f(x):
>         def g(y):
>             return y + once x
>         return g
>
> Does "once" mean not really once here, but "once for each new function
> object that's created for g"?

He specifically wants the latter semantics because it solves the
problem of binding the value of a loop control variable in an outer
scope:

  def f(n):
    return [(lambda: once i) for i in range(n)]

should return n functions returning the values 0 through n-1. Without
the once it returns n identical functions all returning n-1; this is
due to outer-scope references referencing variables, not values. (In
Scheme this is solved by making the for loop create a new variable for
each iteration, but that's not Pythonic.)

> > Right. But there are all sorts of objects that are compared by object
> > identity (e.g. classes, modules, even functions) which may contain
> > mutable components but are nevertheless "constant" for the purpose of
> > switch or optimization. Let's not confuse this concept of constness
> > with immutability.
>
> That's a good point.  We need a concept like "stable for equality"
> separate from "constant", since "constant" and "immutable" will mislead
> those who are used to the meanings of these words in other languages.

Anyone familiar with const in C++ will have a good grasp of the
infinite shades of gray it can express. :-)

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list