Passing data to another class in Python

2020-03-31 05:59发布

问题:

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)

回答1:

I made some changes to the below code, essentially you need to remember:

  1. To use the self keyword when creating object methods.
  2. You need to either pass the data value as a method argument, or assign it somehow (see below).

Here is an example of how to pass data class-to-class:

class CallbackA(object):
    def transmit(self,data):  # See note 1
        do_stuff_with(data)   # See note 1

class CallbackB(object):
    def doRead(self,data):              # see note 1
        self.point_to_A.transmit(data)  # see note 2

class bigClass(object):
    def __init__(self):
        self.A = CallbackA()
        self.B = CallbackB()
        self.B.point_to_A = self.A

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:

test = bigClass()
test.B.doRead(data)

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:

class CallbackA(object):
    def transmit(self,data):
        do_stuff_with(self.data)

class CallbackB(object):
    def __init__(self,data):
        self.data = data

    def doRead(self):
        self.point_to_A.transmit(self.data)

or

class CallbackA(object):
    def transmit(self,data):
        do_stuff_with(self.data)

class ClassB(object):
    def set_data(self,data):
        self.data = data

    def doRead(self):
        self.point_to_A.transmit(self.data)

Note 2: You either need to call the method as: method(class_instance, args) or class_instance.method(args)

EDIT

As an add-on to our discussion in comments. Explicit declaration of point_to_A

 class ClassB(object):
    def __init__(self,A):
        self.point_to_A = A

    def set_data(self,data):
        self.data = data

    def doRead(self):
        self.point_to_A.transmit(self.data)


标签: python oop