[Baypiggies] bound and unbound methods

Ian Zimmerman itz at buug.org
Sun Jun 24 23:39:49 CEST 2012


Zachary> Woah.  Dude, it's just a function.  Why would you need to
Zachary> transform it to a bound method anyways?  How about,

Zachary> meth_cell(self, gunk)

Well, as you all suspect, that wasn't my actual code :-P

Here is something closer.


import SomeReactivePackage as SRP

counter = 1

class Dispatcher(object):

      def handle_ev1(self, evdata):
          self.do_something(evdata)

      def handle_ev2(self, evdata):
          self.do_something_else(evdata)

      handler_table = (
          ('ev1', handle_ev1),
          ('ev2', handle_ev2),
          )

      def __init__(self):
          self.name = 'dispatcher %d' % counter
          counter += 1
          for e, h in self.handler_table:
              # when I leave it like this, the handlers get called with
              # just 1 argument (evdata)
              SRP.register(e, h)

      def do_something(self, evdata):
          print 'Handling ev1: %s: %s' % (self.name, evdata)

      def do_something_else(self, evdata):
          print 'Handling ev2: %s: %s' % (self.name, evdata)


Now, of course I can do this, and it works, I just feel it is ugly:

      def __init__(self):
          handler_table = (
              ('ev1', self.handle_ev1),
              ('ev2', self.handle_ev2),
              )
          self.name = 'dispatcher %d' % counter
          counter += 1
          for e, h in self.handler_table:
              SRP.register(e, h)


So I'm looking for something like this:

      handler_table = (
          ('ev1', self.handle_ev1),
          ('ev2', self.handle_ev2),
          )

      def __init__(self):
          self.name = 'dispatcher %d' % counter
          counter += 1
          for e, h in self.handler_table:
              SRP.register(e, some_python_magic(self, h))


Clearer now?

-- 
Ian Zimmerman
gpg public key: 1024D/C6FF61AD
fingerprint: 66DC D68F 5C1B 4D71 2EE5  BD03 8A00 786C C6FF 61AD
http://www.gravatar.com/avatar/c66875cda51109f76c6312f4d4743d1e.png
Rule 420: All persons more than eight miles high to leave the court.


More information about the Baypiggies mailing list