Different ways of using __init__ for PyQt4

2024/10/5 9:24:15

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__()

Another book does it like this:

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

And yet another does it this way:

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

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?

Answer

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.

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.

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.

https://en.xdnf.cn/q/70504.html

Related Q&A

Numpy: Reshape array along a specified axis

I have the following array:x = np.arange(24).reshape((2,3,2,2)) array([[[[ 0, 1],[ 2, 3]],[[ 4, 5],[ 6, 7]],[[ 8, 9],[10, 11]]],[[[12, 13],[14, 15]],[[16, 17],[18, 19]],[[20, 21],[22, 23]]]])I wou…

python suds wrong namespace prefix in SOAP request

I use python/suds to implement a client and I get wrong namespace prefixes in the sent SOAP header for a spefic type of parameters defined by element ref= in the wsdl. The .wsdl is referencing a data …

Allow help() to work on partial function object

Im trying to make sure running help() at the Python 2.7 REPL displays the __doc__ for a function that was wrapped with functools.partial. Currently running help() on a functools.partial function displ…

How To Fix Miscased Procfile in Heroku

Heroku will not reload my corrected ProcfileI have ran git status which shows me the Procfile and I realized that I spelled Procfile with a lower case p. I saw the error and updated the file name in my…

Using Pythons xml.etree to find element start and end character offsets

I have XML data that looks like:<xml> The captial of <place pid="1">South Africa</place> is <place>Pretoria</place>. </xml>I would like to be able to extra…

How to get public key using PyOpenSSL?

Im tring to create python script, that would take PKCS#12 package and print some information contained in x509 certificate and using for this purpouses PyOpenSSL module. So far i want to fetch from cer…

what is the best way to extract data from pdf

I have thousands of pdf file that I need to extract data from.This is an example pdf. I want to extract this information from the example pdf.I am open to nodejs, python or any other effective method. …

Get random key:value pairs from dictionary in python

Im trying to pull out a random set of key-value pairs from a dictionary I made from a csv file. The dictionary contains information for genes, with the gene name being the dictionary key, and a list of…

UnicodeDecodeError: ascii codec cant decode byte 0xc5

UnicodeDecodeError: ascii codec cant decode byte 0xc5 in position 537: ordinal not in range(128), referer: ...I always get this error when I try to output my whole website with characters "č"…

wpa-handshake with python - hashing difficulties

I try to write a Python program which calculates the WPA-handshake, but I have problems with the hashes. For comparison I installed cowpatty (to see where I start beeing wrong).My PMK-generation works …