如何在 Python 中引用创建者对象? [英] How to reference creator object in Python?

查看:36
本文介绍了如何在 Python 中引用创建者对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.

有两个类:A 和 B.A 创建了类型 B 的对象.所以从 A 访问 B 的方法很容易,但是如何从对象 B 访问对象 A 的方法?我正在尝试在 Qt 中使用双向信号槽机制.A 很容易在 B 中有一个信号槽,但 B 看不到 A 中的信号.

There are two classes: A and B. A creates object of type B. So from A, it is easy to access methods of B, but how can I access methods of object A from object B? I am trying to use bidirectional signal-slot mechanism in Qt. A can easily have a slot for a signal in B, but B cannot see signals in A.

我有一个模糊的想法,即在创建对象时可以以某种方式将创建者类引用为成员",但我不确定如何对此进行编码.

I have a vague idea that somehow the creator class can be referenced as a 'member' while an object is being created, but I am not sure how to do code this.

提前致谢.

推荐答案

将创建者作为参数传递给构造函数:

Pass the creator as an argument to the constructor:

class B(object):
    def __init__(self, creator):
        self._creator = creator   # or do something else with it

用作:

class A(object):
    def somemethod(self):
        b = B(self)

这篇关于如何在 Python 中引用创建者对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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