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

Jan Kaliszewski zuo at chopin.edu.pl
Tue Oct 4 01:47:04 CEST 2011


Nick Coghlan dixit (2011-10-01, 22:11):

> Now is better than never.
> Although never is often better than *right* now.
>   - The status quo has served us well for a long time. If someone can
> come up with an elegant syntax, great, let's pursue it.

I believe that both syntax propositions:

    def spam(x, some, arguments, foo_bar=997) [variable=1, lock=Lock()]:
        nonlocal variable
        with lock:
            variable += x
        return variable + foo_bar

and

    @(variable=1, lock=Lock())
    def spam(x, some, arguments, foo_bar=997):
        nonlocal variable
        with lock:
            variable += x
        return variable + foo_bar

-- are quite elegant and each of them would be nice and useful
equivalent of:

    def _temp():
        variable = 1
        lock = Lock()
        def spam(x, some, arguments, foo_bar=997):
            nonlocal variable
            with lock:
                variable += x
            return variable + foo_bar
        return spam
    spam = _temp()
    del _temp

> Otherwise,
> this whole issue really isn't that important in the grand scheme of
> things (although a PEP to capture the current 'state of the art'
> thinking on the topic would still be nice - I believe Jan and Eric
> still plan to get to that once the discussion dies down again)

Yes, I already started creating such a summary, though this thread
changed a lot (at least in my mind -- about the subject). As I had
reserved I cannot promise to do it quickly (because of other
activities).

Cheers.
*j




More information about the Python-ideas mailing list