简体   繁体   English

如何扩展一个类实例?

[英]How to extend a class instance?

I have an instance.. 我有一个实例..

groupCell = QtGui.QGroupBox()
print groupCell.title() #this class has a method title()

I am not able to change anything of this instance, it comes how it is... 我无法更改此实例的任何内容,它的状态如何……

I need to extend this instance (add some methods etc.) 我需要扩展此实例(添加一些方法等)

class GroupBoxWithCheckbox (QtGui.QGroupBox):
    def __init__(self, basegroupbox, checkbox):
        #something like self = basegroupbox ?
        self.checkbox = checkbox
    def method(self):
        pass

and finally 最后

groupCellWithCheckBox = GroupBoxWithCheckbox(groupCell, checkbox)
print groupCellWithCheckBox.title()

I have to get the same title as with groupCell. 我必须获得与groupCell相同的标题。

You can define a new class extending QtGui.QGroupBox that looks like this: 您可以定义一个扩展QtGui.QGroupBox的新类,如下所示:

class GroupBoxWithCheckbox(QtGui.QGroupBox):

    def __init__(self, checkbox):
        super(GroupBoxWithCheckbox, self).__init__()
        self.checkbox = checkbox

    def method(self):
        pass

Then you can simply make groupCell an instance of this class, and pass in a checkbox when you initialise it: 然后,你可以简单地groupCell 这个类的一个实例,并传递一个复选框,当你初始化它:

groupCell = GroupBoxWithCheckbox(checkbox)

That will have the same effect as what you are trying to do here. 这将与您在此处尝试执行的操作具有相同的效果。


Edit, since new information has been provided: 编辑,因为已经提供了新信息:

Since we're talking Python here, you can dynamically add things to any instance you want. 由于我们在这里谈论Python,因此您可以将内容动态添加到所需的任何实例中。 It's totally possible to do this: 完全有可能这样做:

groupCell.checkbox = checkbox

Even if the groupCell doesn't have a checkbox property . 即使groupCell没有checkbox属性 The property will be added when you set it, as in my snippet above. 如上我的代码段所示,该属性将在您设置时添加。 You could use that to do what you want. 您可以用它来做您想要的。 It's kind of a weird thing to do, and I don't recommend it, but it would work. 这样做很奇怪,我不建议这样做,但是可以 The alternative is to make a wrapper class of some sort: 另一种方法是制作某种包装器类:

class GroupBoxWithCheckbox(object):
    def __init__(self, groupbox, checkbox):
        self.groupbox = groupbox
        self.checkbox = checkbox

groupCell = GroupBoxWithCheckbox(groupCell, checkbox)

And then any time you want to access a method of the original GroupBox, you can do something like 然后,只要您想访问原始GroupBox的方法,就可以执行以下操作

groupCell.groupbox.title()

groupCell.groupbox will contain all of the methods that the original GroupBox did, but you'll also have access to groupCell.checkbox . groupCell.groupbox将包含原始GroupBox所做的所有方法,但您还可以访问groupCell.checkbox

The latter solution is what I would implement if I were coding this. 如果我对此进行编码,则将实现后一种解决方案。

Call base class constructor using super : 使用super调用基类构造函数:

class GroupBoxWithCheckbox(QtGui.QGroupBox):
    def __init__(self, basegroupbox, checkbox):
        super(GroupBoxWithCheckbox, self).__init__()
        self.checkbox = checkbox
        self.basegroupbox = basegroupbox
    def title(self):
        return self.basegroupbox.title()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM