Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:22:55+00:00 2026-05-16T04:22:55+00:00

In my Python app, I’m using events to communicate between different plugins. Now, instead

  • 0

In my Python app, I’m using events to communicate between different plugins.
Now, instead of registering the methods to the events manually, I thought I might use decorators to do that for me.

I would like to have it look like this:

@events.listento('event.name')
def myClassMethod(self, event):
    ...

I have first tried to do it like this:

def listento(to):
    def listen_(func):
        myEventManager.listen(to, func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return func
    return listen_

When I callmyEventManger.listen('event', self.method)from within the instance, everything is running fine. However, if I use the decorator approach, theselfargument is never passed.

The other approach that I have tried, after searching for a solution on the Internet, is to use a class as a decorator:

class listen(object):
    def __init__(self, method):
        myEventManager.listen('frontend.route.register', self)
        self._method = method
        self._name = method.__name__
        self._self = None

    def __get__(self, instance, owner):
        self._self = instance
        return self

    def __call__(self, *args, **kwargs):
        return self._method(self._self, *args, **kwargs)

The problem with this approach is that I don’t really understand the concept of__get__, and that I don’t know how I’d incorporate the parameters.
Just for testing I have tried using a fixed event to listen to, but with this approach, nothing happens. When I add print statements, I can see that__init__is called.
If I add an additional, “old style” event registration, both__get__and__call__get executed, and the event works, despite the new decorator.

What would be the best way to achieve what I’m looking for, or am I just missing some important concept with decorators?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:22:55+00:00Added an answer on May 16, 2026 at 4:22 am

    The decorator approach isn’t working because the decorator is being called when the class is constructed, not when the instance is constructed. When you say

    class Foo(object):
      @some_decorator
      def bar(self, *args, **kwargs):
        # etc etc
    

    then some_decorator will be called when the class Foo is constructed, and it will be passed an unbound method, not the bound method of an instance. That’s why self isn’t getting passed.

    The second method, on the other hand, could work as long as you only ever create one object of each class you use the decorator on, and if you’re a bit clever. If you define listen as above and then define

    class Foo(object):
      def __init__(self, *args, **kwargs):
        self.some_method = self.some_method # SEE BELOW FOR EXPLANATION
        # etc etc
      @listen
      def some_method(self, *args, **kwargs):
        # etc etc
    

    Then listen.__get__ would be called when someone tried to call f.some_method directly for some f…but the whole point of your scheme is that no-one’s doing that! The event call back mechanism is calling the listen instance directly ’cause that’s what it gets passed and the listen instance is calling the unbound method it squirrelled away when it was created. listen.__get__ won’t ever get called and the _self parameter is never getting set properly…unless you explicitly access self.some_method yourself, as I did in the __init__ method above. Then listen.__get__ will be called upon instance creation and _self will be set properly.

    Problem is (a) this is a horrible, horrible hack and (b) if you try to create two instances of Foo then the second one will overwrite the _self set by the first, because there’s still only one listen object being created, and that’s associated to the class, not the instance. If you only ever use one Foo instance then you’re fine, but if you have to have the event trigger two different Foo‘s then you’ll just have to use your “old style” event registration.

    The TL,DR version: decorating a method decorates the unbound method of the class, whereas you want your event manager to get passed the bound method of an instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using only PHP you could do something as simple as:… May 17, 2026 at 2:26 am
  • Editorial Team
    Editorial Team added an answer If I've understood your requirements correctly, you're looking to do… May 17, 2026 at 2:26 am
  • Editorial Team
    Editorial Team added an answer Use an abstract/virtual property instead of a field: abstract class… May 17, 2026 at 2:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In my Python app, I have an XML document that I'd like to transform
I'm creating a python app for google app engine and I've got a performance
I have a python app running fine on Windows, Linux and Mac which I
I have a Python app. It loads config files (and various other files) by
I wrote a python app and it needs python2.6. I'm trying to get it
I currently have a Python app I am developing which will data carve a
I am developing a Python App Engine app, where I want to split the
Consider a GAE (python) app that lets users comment on songs. The expected number
I have a python app which is supposed to be very long-lived, but sometimes
In the python app engine docs, I see something called dbReferenceProperty. I can't understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.