On Thu, May 27, 2021 at 11:38 PM Paul Moore <p.f.mo...@gmail.com> wrote:
>
> This reminds me, if we ignore the performance aspect, function
> attributes provide this functionality, but there's a significant
> problem with using them because you can't access them other than by
> referencing the *name* of the function being defined.

Yeah, I mentioned that earlier, but just as one of the wide variety of
variously-clunky ways to achieve the same thing. I think it's
semantically the closest, but defining the initial value *after* the
function is pretty unexciting.

> It would be nice to have a better way to reference function attributes
> from within a function. (This would also help write recursive
> functions that could be safely renamed, but I'm not sure many people
> would necessarily think that's a good thing ;-))

The interaction with renaming isn't particularly significant, but the
interaction with decoration is notable. Inside the execution of a
function, you'd have a reference to the innermost function, NOT the
one that would be identified externally. Whether that's a good thing
or a bad thing remains to be seen...

Hmm.

def static(**kw):
    def deco(func):
        statics = types.SimpleNamespace(**kw)
        @functools.wraps(func)
        def f(*a, **kw):
            func(*a, **kw, _statics=statics)
        return f
    return deco

@statics(n=0)
def count(*, _statics):
    _statics.n += 1
    return _statics.n

Add it to the pile of clunky options, but it's semantically viable.
Unfortunately, it's as introspectable as a closure (that is: not at
all).

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JRDLOROGRHBSRMY3DF2E3VHB3ANIDKRT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to