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

  • SEARCH
  • Home
  • 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 195309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:40:12+00:00 2026-05-11T16:40:12+00:00

I need to write handlers for several different case types (in Python). The interface

  • 0

I need to write handlers for several different case types (in Python). The interface for all this types are the same, but the handling logic is different.

One option would be defining a common class that receives the particular handler type as one of the __init__ parameters:

class Handler:
   def __init__ (self, handlerType):
       self._handlerType = handlerType
       self._handler = handlerType.handleStuff

   def handleStuff(self, *args, **kwargs):
       return self._handler(args, kwargs)


# case specific handlers

class Handler_Case1:
   def handleStuff(self, *args, **kwargs):
       print 'Handling Case 1'


class Handler_Case2:
   def handleStuff(self, *args, **kwargs):
       print 'Handling Case 2'



if __name__ == '__main__':
   handlers = []
   handlers.append(Handler(Handler_Case1))
   handlers.append(Handler(Handler_Case2))
   for h in handlers:
       h.handleStuff()

However, this results in a TypeError:

TypeError: unbound method handleStuff() must be called with Handler_Case1 instance as first argument (got tuple instance instead)

Another option is to mimic abstract function, as shown here(“Q: Can you implement abstract classes in Python in 0 lines of code?”):

class Handler:
  def handleStuff(self, *args, **kwargs): abstract
  def commonFunction(self):
       print 'Common function'


 # case specific handlers

 class Handler_Case1(Handler):
  def handleStuff(self, *args, **kwargs):
      print 'Handling Case 1'


 class Handler_Case2(Handler):
  def handleStuff(self, *args, **kwargs):
      print 'Handling Case 2'



 if __name__ == '__main__':
  handlers = []
  h1 = (Handler_Case1())
  h2 = (Handler_Case2())
  handlers.append(h1)
  handlers.append(h2)
  for h in handlers:
      h.handleStuff()
      print

So, actually, I have two questions:

  1. Which of the two approaches is more pythonic?
    and
  2. How to implement the first one?
  • 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-11T16:40:12+00:00Added an answer on May 11, 2026 at 4:40 pm

    I might be missing some subtle intricacy in your question, but given your first example, what precludes you from doing something like this:

    class HandlerCase1(object):
        def handle_stuff(self, *args, **kwargs):
            print "Handling case 1"
    
    
    class HandlerCase2(object):
        def handle_stuff(self, *args, **kwargs):
            print "Handling case 2"
    
    
    if __name__ == "__main__":
        handlers = []
        handlers.append(HandlerCase1())
        handlers.append(HandlerCase2())
        for h in handlers:
            h.handle_stuff()
    

    And if you want the classes to share some common (base) functionality, is there something keeping you from doing this:

    class Handler(object):
        def common_function(self):
            print "Common function"
    
    
    class HandlerCase1(Handler):
        def handle_stuff(self, *args, **kwargs):
            print "Handling case 1"
    
    
    class HandlerCase2(Handler):
        def handle_stuff(self, *args, **kwargs):
            print "Handling case 2"
    
    
    if __name__ == "__main__":
        handlers = []
        handlers.append(HandlerCase1())
        handlers.append(HandlerCase2())
        for h in handlers:
            h.handle_stuff()
            h.common_function()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • 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 Jakob Nielsen's "useit" site is a great resource for web… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer http://ant.apache.org/ Here is how to do C++ projects with Ant:… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer Fun with extension methods... var ret = john.ToXmlString() public static… May 12, 2026 at 12:41 am

Related Questions

IIS enables us to also configure Asp.Net file mappings. Thus besides aspx, IIS also
I am developing a framework for various in-house CRUD apps. I've considered several MS
So right now i need to create and implement an extension of the Python
I have a question about events in .NET (C#). I have had to write

Trending Tags

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

Top Members

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.