在 PyQt4 中使用 __init__ 的不同方式 [英] Different ways of using __init__ for PyQt4

查看:48
本文介绍了在 PyQt4 中使用 __init__ 的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我正在尝试使用 PyQt4 从基本的 Python 转向一些 GUI 编程.我正在看几本不同的书籍和教程,它们似乎每本都以稍微不同的方式启动类定义.

So... I'm working on trying to move from basic Python to some GUI programming, using PyQt4. I'm looking at a couple different books and tutorials, and they each seem to have a slightly different way of kicking off the class definition.

一个教程从这样的课程开始:

One tutorial starts off the classes like so:

class Example(QtGui.QDialog):
    def __init__(self):
        super(Example, self).__init__()

另一本书是这样的:

class Example(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)

还有一个是这样做的:

class Example(QtGui.QDialog):
    def__init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

我仍然试图围绕类和 OOP 以及 super() 和所有...我认为第三个示例的最后一行或多或少完成了我的想法是否正确通过直接显式调用基类,与前面使用 super() 的调用相同吗?对于诸如此类的相对简单的示例,即单继承,使用一种方式与另一种方式是否有任何真正的好处或理由?最后......第二个例子将 parent 作为参数传递给 super() 而第一个没有......关于为什么/何时/在哪里的任何猜测/解释会合适吗?

I'm still trying to wrap my mind around classes and OOP and super() and all... am I correct in thinking that the last line of the third example accomplishes more or less the same thing as the calls using super() in the previous ones, by explicitly calling the base class directly? For relatively simple examples such as these, i.e. single inheritance, is there any real benefit or reason to use one way vs. the other? Finally... the second example passes parent as an argument to super() while the first does not... any guesses/explanations as to why/when/where that would be appropriate?

推荐答案

第一个根本不支持将 parent 参数传递给其基类.如果你知道你永远不需要 parent 参数,那很好,但它不太灵活.

The first one simply doesn't support passing a parent argument to its base class. If you know that you'll never need the parent arg, that's fine, but this is less flexible.

由于这个例子只有单继承,super(Example, self).__init__(parent)QtGui.QDialog.__init__(self, parent)完全一样>;前者使用 super 来获取 self 的版本",它调用 QtGui.QDialog 的方法而不是 Example>'s,这样self就会自动包含进来,而后者直接调用函数QtGui.QDialog.__init__并显式传递selfparent 参数.在单继承中,除了键入数量以及如果更改继承必须更改类名这一事实之外,AFAIK 没有区别.在多重继承中,super 半智能地解析方法.

Since this example only has single inheritance, super(Example, self).__init__(parent) is exactly the same as QtGui.QDialog.__init__(self, parent); the former uses super to get a "version" of self that calles QtGui.QDialog's methods instead of Example's, so that self is automatically included, while the latter directly calls the function QtGui.QDialog.__init__ and explicitly passes the self and parent arguments. In single inheritance there's no difference AFAIK other than the amount of typing and the fact that you have to change the class name if you change inheritance. In multiple inheritance, super resolves methods semi-intelligently.

第三个例子实际上使用了QWidget而不是QDialog,这有点奇怪;大概是因为 QDialogQWidget 的子类并且在它的 __init__ 中没有做任何有意义的事情,但我不确定.

The third example actually uses QWidget instead of QDialog, which is a little weird; presumably that works because QDialog is a subclass of QWidget and doesn't do anything meaningful in its __init__, but I don't know for sure.

这篇关于在 PyQt4 中使用 __init__ 的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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