[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Eric Snow ericsnowcurrently at gmail.com
Wed Sep 28 22:30:19 CEST 2011


On Wed, Sep 28, 2011 at 1:38 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
> On 28 September 2011 19:08, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> [...]
>> The Def-time Value Approach
>> ------------------------------------
>>
>> For def-time values, the values would have to be stored in a function
>> attribute similar to __defaults__ and __kwdefaults__.  Let's call it
>> __spam__.  A keyword (not "nonlocal" <wink>) in the function body
>> would indicate that the name should be initialized with each call to
>> the appropriate value in __spam__.  Then we would have a (builtin?)
>> decorator factory that would update values in __spam__:
>>
>> #<example>
>>
>> def init_deftime(**kwargs):
>>    def decorator(f):
>>        for name in kwargs:
>>            index = figure_out_index(f, name)
>>            f.__spam__[index] = kwargs[name]
>>            #functools.update_spam(f, name, kwargs[name])
>>        return f
>>    return decorator
>>
>> @init_deftime(x=5):
>> def f():
>>    some_keyword x
>>    print(x)
>
> Mmh.  This looks exactly like one I posted earlier on:
> http://mail.python.org/pipermail/python-ideas/2011-September/011892.html

Yeah, very similar.  Somehow I missed your post earlier.  It seems
like a decent approach.  It is essentially a variation on how default
arguments work right now, using the decorator instead of the function
signature.

-eric

>
> --
> Arnaud
>



More information about the Python-ideas mailing list