On Thu, 27 May 2021 at 15:04, Chris Angelico <ros...@gmail.com> wrote:
> 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).

Still arguably clunky, still doesn't have any performance benefits,
but possibly a better interface to function attributes than just using
them in their raw form.

def static(**statics):
    def deco(func):
        for name, value in statics.items():
            setattr(func, name, value)
        func.__globals__["__me__"] = func
        return func
    return deco

@static(a=1)
def f():
    print(__me__.a)

Paul
_______________________________________________
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/SILEUKPYQSJN6ISHRQA2GBSZ2JDR7WEH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to