views:

337

answers:

2

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?
+2  A: 

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()
mipadi
+1  A: 

The problem with the first one is that to call the handler, you need to have an instance of the handler type first. You are trying to call a method on the class without an instance.

That is, say you have

class foo:
    def bar(self):
        self.data = "Foobar"

What you have done basically does this

foo.bar()

While you are supposed to do this

instance = foo()
instance.bar()

Or to keep it short

foo().bar()

And that is why you got the TypeError. Now besides mipadi's suggestion, if you really need to implement the first method, here is one way to do it.

class Handler:
    """
    What this will do is, make and store an instance of
    the handlerType, and call the handleStuff method on
    that instance.
    """
    def __init__ (self, handlerType):
        # Create an instance of the handlerType
        self.handler = handlerType()

    def handleStuff(self, *args, **kwargs):
        # Call the handleStuff method on the handler instance
        return self.handler.handleStuff(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()
abhinavg