I am using Twisted and have a couple of callbacks, both of different types (so they don't share a factory). I am trying to get data from one callback object to another:
class CallbackA(object):
def transmit(self, data):
self.sendMessage(self, data)
class CallbackB(object):
def doRead(self): # this is called by Twisted
self.point_to_A.transmit(self.point_to_A, data)
class bigClass(object):
A = aClass
B = bClass
self.B.point_to_A = self.A
Now, I was able to get this to work by using @staticmethod
before transmit
. Then I added the method (from twisted) self.sendMessage
. I got an error: 'global name self is not defined'. Ok. So I passed the object as the first argument like this:
self.point_to_A.transmit(self.point_to_A, data)
Then I get an error like this: 'unbound method sendMessage() must be called with 'class A' instance as first argument (got classobj instance instead)
So it seems I don't understand how to pass data to functions between sub-objects (objects instantiated under the same parent object); twisted callbacks in this case.
How can I call a function in child object 'A' from child object 'B', and pass data to that function, such that 'self' is available in that function in 'A' (as required by the Twisted method being used)?
Edit: Updated to show the 'self' references (as suggested in the comments), the error I get with the code as above is: unbound method sendMessage() must be called with aClass
instance as a first argument (got classobj instance instead)
I made some changes to the below code, essentially you need to remember:
self
keyword when creating object methods.data
value as a method argument, or assign it somehow (see below).Here is an example of how to pass data class-to-class:
the above code will probably not work verbatim but is an example of how you could pass data to another class in the way you describe. You should be able to get yours working from this example.
And then when it is called:
Note 1: You need to declare the
data
somehow. Either pass it as a method argument as I have shown, or you need to declare it as a property:or
Note 2: You either need to call the method as:
method(class_instance, args)
orclass_instance.method(args)
EDIT
As an add-on to our discussion in comments. Explicit declaration of
point_to_A