[Python-ideas] Before and after the colon in funciton defs.

Carl Matthew Johnson cmjohnson.mailinglist at gmail.com
Thu Sep 22 20:49:53 CEST 2011

On Sep 22, 2011, at 3:58 AM, Nick Coghlan wrote:

>  (As an implementation detail, this would likely
> work by assigning such expressions an index in the function's closure
> list and populating them at definition time as cells)

This reminds me that there was a proposal a few weeks back to be able to refer to the function one is in absolutely. Right now, you can't just slap an attribute onto f in a loop and then get it back at exec time, since the variable f will refer to the last f defined in the loop. But suppose we had some sort of "THIS_FUNCTION" magic variable… Then this would work:

fs = []
for i in range(10):
    def f():
        return THIS_FUNC.value
    f.value = i

[f() for f in fs]

(Again, it has to be some sort of magical variable, since "return f.value" will refer to the 9 producing function at the end.)

This could then be combined with a standard inject decorator.

    def f():
        return THIS_FUNC.inject_values.i

In cases where you're not in a loop or something, you could just write "f.values.i" instead of using the new magical variable.

More information about the Python-ideas mailing list