将数据传递给Python中的另一个类 [英] Passing data to another class in Python

查看:304
本文介绍了将数据传递给Python中的另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Twisted并有几个不同类型的回调(因此它们不共享工厂).我正在尝试将数据从一个回调对象传递到另一个:

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

现在,我可以通过在transmit之前使用@staticmethod使其工作.然后,我添加了方法(从扭曲)self.sendMessage.我收到一个错误:未定义全局名称self".好的.因此,我将对象作为第一个参数传递,如下所示:

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)

然后我得到一个类似的错误:'未绑定方法sendMessage()必须以'class A'实例作为第一个参数来调用(取而代之的是class classobj实例)

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.

如何从子对象"B"中调用子对象"A"中的函数,并将数据传递给该函数,使得该函数中的"self"在"A"中可用(如Twisted方法所要求)被使用)?

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)?

已更新为显示自我"引用(如注释中所建议),我在上面的代码中遇到的错误是:必须使用实例作为第一个参数(取而代之的是classclassobj实例)

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:

  1. 在创建对象方法时使用self关键字.
  2. 您需要将data值作为方法参数传递,或以某种方式进行分配(请参见下文).
  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.

然后当它被调用时:

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

注释1:您需要以某种方式声明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)

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)

注释2:您需要以以下方式调用该方法:method(class_instance, args)class_instance.method(args)

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

编辑

作为我们在评论中讨论的附加内容. point_to_A

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中的另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆