python - Decorating a method -
In my Python app, I am using events to communicate between different plugins. Now, instead of manually recording the methods of events, I thought I could use the decorator to do this for me.
I want to look like this:
@ Events.listento ('event.name') def myClassMethod (auto, incident): ...
< / Pre>I have tried to do the following:
When I call myEventManger.listen ('event', self.method)
Inside the example, everything is going fine. However, if I use decorator approach, then self
is never passed.
After searching for a solution on the Internet, use whatever other effort I have made, use a class as a decorator:
listen to the class (object ): Def __init __ (self, method): myEventManager.listen ('frontend.route.register', self) self._method method = ._name = method .__ name___self_self = any deff DD ___ race (___ Self, example, owner): self._self = examples itself back self-def __call __ (self, * args, ** kwargs): return self The problem with this approach is that I do not really understand the concept of __get_ Found, and I do not know how do I add parameters to just testing I've tried to use a certain event, but from this point of view, nothing happens when I add a print statement, I can see That is called __init __
. If I add an additional, "old style" event registration, in spite of both new codecs, __ get __
and __ call __
are executed, and event work Does. > What am I seeing, what would be the best way, or am I just remembering some important concept with decorators?
The decorator approach is not working because the decorator is being asked to build a class, Examples are not at the time of creation. When you say
square fu (object): @some_decorator def bar (self, * args, ** kwargs): # etc etc
then some_decorator
will be called when Class Fu is created, and it will be passed to an unbound method, not an exemplary method. This is the reason that itself
is not being passed. On the other hand, on the other hand, unless you only make a object, you can use the decorator of each category, and if you are a little clever If you define the above, then listen
and then define
square fu (object): def __init __ (self, * args, ** Kwargs): see below @listen def some_method (self, * args, ** kwargs) for self.some_method = self .some_method #impressions # etc etc.: # etc etc.
then
listen_date___
when someone is trying to callf.some_method
directly to somef
... but the whole point of your plan is that no one is doing this! Calling the event call back mechanism directly to the call because this is it andlisten
the example is calling the unbound method when it was created when it was created I went.listen.__ go __
will never call and the_self
parameter is never set properly ... unless you are clear Asaccess .some_method
itself, as I did in the above method I__init __
. Thenlisten.__ get __
will be called on the creation of the example and will be set correctly_self
.The problem is (A) this is a terrible, terrible hack and (b) if you try to create two examples of
Foo
second, first,_self
, because still only alisten
object is being created, and it is linked to class, for example if you ever have afoo
example, you are okay, but if you want to trigger the event of two differentfoo
You must use your "old style" event registration.TL, DR version: A decorating method is decorated with the unbound class, while you want your event manager forced one Example method.
Comments
Post a Comment