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 7908745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:05:08+00:00 2026-06-03T12:05:08+00:00

I am building an event dispatcher framework that decodes messages and calls back into

  • 0

I am building an event dispatcher framework that decodes messages and calls back into user code. My C++ background suggests that I write:

class Handler:
    def onFoo(self):
        pass
    def onBar(self):
        pass

class Dispatcher:
    def __init__(self):
        self.handler = Handler()

    def run():
        while True:
            msg = decode_message() # magic
            if msg == 'foo':
                handler.onFoo()
            if msg == 'bar':
                handler.onBar()

Then the framework user would write something like:

class MyHandler(Handler):
    def onFoo(self):
        do_something()

dispatcher = Dispatcher()
myHandler = MyHandler()
dispatcher.handler = myHandler
dispatcher.run()

But I can also imagine putting onFoo() and onBar() as methods of Dispatcher and letting the user replace them with other methods. Then the user’s code would look like:

def onFoo():
    do_something()

dispatcher = Dispatcher()
dispatcher.onFoo = onFoo

I could also make Dispatcher.onFoo a list of callables, so the user could attach more than one handler like in C#.

What is the most Pythonic way to do it?

  • 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-06-03T12:05:10+00:00Added an answer on June 3, 2026 at 12:05 pm

    I wouldn’t say that there’s anything particularly wrong about doing it the first way, especially if you want to allow Dispatcher objects to be customized in an organized way at runtime (i.e. by passing different kinds of Handler objects to them), if your Handlers need to interact with and maintain complex state.

    But you don’t really gain anything by defining a base Handler class this way; a class could still subclass Handler without overriding any methods, because this isn’t an abstract base class — it’s just a regular base class. So if there are some sensible default behaviors, I would suggest building them in to Handler. Otherwise, your users don’t gain anything from Handler at all — they might as well just define their own Handler class. Though if you just want to provide a no-op placeholder, then this set-up is fine.

    In any case, I would personally prefer the first approach to the second that you suggest; I’m not certain that either is more “pythonic,” but the first seems like a cleaner approach to me, since it keeps Handler and Dispatcher logic separate. (It avoids situations like the one you see in threading.Thread where you can only safely override some methods — I’ve always found that a bit jarring.)

    I feel I should point out, though, that if you really want a bonafide abstract base class, you should write one! As of 2.6, Python provides support for flexible abstract base classes with lots of nice functionality. For example, you can define an abstract method with the abstractmethod decorator, which ensures that it must be overridden by the subclass; but you can also define regular methods that don’t have to be overridden. If you’re seeking a Pythonic version of the c++ idiom, this is probably the best way to go — it’s not the same thing, of course, but it comes closer than what you have now.

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

Sidebar

Related Questions

I'm building an application that needs to fire an event every second, but it
I am building a web application that will pull event logs from multiple servers
I'm building a search using ext-js. I have an event that fires on keyup.
I am building a WPF application that calls web services and displays the data
I am building a code behind page that has a public property (MyDTOItem) which
I am working on a building a discrete event simulator. Wikipedia mentioned that there
I'm building a custom component and I need to convert the event that is
I'm building an auto-follow div that is bound to the $(window).scroll() event. Here is
I'm building a framework which relies heavily on user-registered events, All I really wanted
I'm building a music events website and want to have a 'share this event'

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.