0

I'm using iPython right now to interactively set up a Twisted network. The script that I run in iPython describes best of what I have to do:

import router, pdb

# creates nodes which encapsulate RandomVector and VectorAdder objects
a = router.LocalNode(router.RandomVector, '/topic/a_c')
b = router.LocalNode(router.RandomVector, '/topic/b_c')
c = router.LocalNode(router.VectorAdder)
c.registerAsListener('/topic/a_c')
c.registerAsListener('/topic/b_c')

def conn():
    global a
    global b
    a.onConnect()
    b.onConnect()


router.r.loadOnConnect(conn)
router.STOMPconnect(router.r)
router.reactor.run()

What's happening is that conn() is being passed to a Twisted protocol, which runs conn() every time the connection is lost.

onConnect looks like this:

def onConnect(self):
    self._lc = LoopingCall(self.advance)
    self._lc.start(1)

Whenever onConnect gets run, I get the error:

TypeError: 'unbound method onConnect() must be called with RandomVector instance as first argument (got nothing instead)'

Does anyone know why this is happening?

1
  • 1
    You don't need the global declarations in conn() since you are not assigning to the global variables. Mar 5, 2010 at 17:17

1 Answer 1

2

I don't think this is a scoping issue. Are you sure you don't need to use:

a = router.LocalNode(router.RandomVector(), '/topic/a_c')
b = router.LocalNode(router.RandomVector(), '/topic/b_c')

i.e. instantiate the RandomVector you pass to LocalNode?

This recommendation is triggered by the Unboud method error message : an unbound method is one which is linked to a class and not to an instance / object. As your message complains about a.onConnect being an unbound method, it looks like you need to pass an instance and not a class.

1
  • 1
    This sounds like the right answer to me. It might help to explain a bit more about what an unbound method in Python is, though. Mar 6, 2010 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.