codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


On 04/09/2019 15:21, Antoon Pardon wrote:
> 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?

I can't see a way of doing that directly, but you could cheat by putting 
the "class variables" in a global dictionary?  And perhaps referencing 
that global from a class variable?  Something like:

my_class_registry = {}

class MyClass(...):
     MyClassRegistry = my_class_registry

     @register(my_class_registry)
     def MyFunction(...):
         ...

Or you may be able to achieve what you want with dir() and some careful 
naming?

-- 
Rhodri James *-* Kynesim Ltd