How do I give a decorator acces to the class of a decorated function

Chris Angelico rosuav at gmail.com
Thu Sep 5 15:00:09 EDT 2019


On Fri, Sep 6, 2019 at 4:33 AM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> 04.09.19 17:21, Antoon Pardon пише:
> > What I am trying to do is the following.
> >
> > class MyClass (...) :
> >      @register
> >      def MyFunction(...)
> >          ...
> >
> > What I would want is for the register decorator to somehow create/mutate
> > class variable(s) of MyClass.
> >
> > Is that possible or do I have to rethink my approach?
> >
>
> You can make register() returning a descriptor with the __set_name__()
> method.

Was not aware of that. Cool! For those not familiar with it:

https://docs.python.org/3/reference/datamodel.html#object.__set_name__

ChrisA



More information about the Python-list mailing list