[Python-Dev] once [was: Simple Switch statementZ] (original) (raw)

Jim Jewett jimjjewett at gmail.com
Wed Jun 28 19:17:09 CEST 2006


On 6/28/06, Guido van Rossum <guido at python.org> wrote:

def indexfunctions(n): return [(lambda i=i: i) for i in range(n)]

which works but has the disadvantage of returning a list of functions of 0 or 1 argument

I believe at least one poster has pointed out that 'once' (if defined suitably) could be used as a better way to do this:

Cleaner, yes. But you would still have to remember the once, just as you have to remember the i=i, so I don't think it would actually save any confusion in practice.

Another alternative might be letting functions get at themselves, rather than just their names. (Methods can save attributes on self, but functions are out of luck if someone else reused their name.)

Perhaps 'once' is too misleading a name, given the confusion you alluded to earlier. Maybe we could use 'capture' instead? A capture expression would be captured at every function definition time, period.

I think it would have the same problem; I would still want to read that as "The first time you run this, capture the result.", rather than "Capture the binding current at funcdef time, even though you're skipping all the other statements at this indent level."

Capture expressions outside functions would be illegal or limited to compile-time constant expressions (unless someone has a better idea).

At a minimum, it should be able to capture the expression's current value at load-time, which might well involve names imported from another module.

A capture expression inside "if 0:" would still be captured to simplify the semantics (unless the compiler can prove that it has absolutely no side effects).

Running code that was guarded by "if 0:" sounds like a really bad idea.

-jJ



More information about the Python-Dev mailing list