When should I use "parent=None" in __ini__ and "parent" in super()

Mohsen Owzar mohsen.owzar at gmail.com
Thu Sep 23 23:38:50 EDT 2021


Hi Guys
I'm writing since almost one-year codes in Python, using TKinter and PyQt5.
I'm somehow able to writes GUIs in both of them.
But since I'm using more Pyqt5 and using classes with initialization and super() constructs, and also I saw lots of videos and examples of coding them, I still don’t know exactly how and when should I use the parent in __init()__ and super() construct.
For example, the following lines use "parent=None" in the __init__ construct and "parent" in the super() construct. And sometimes I do not see any of them in these two constructs.
::::::::::::::::::::::::::::::::::::::::::::::::
class MyClass1(QWidget):
    def __init__(self, name, parent=None):
        super(MyClass1, self).__init__(parent)
        print(self.parent().test)
::::::::::::::::::::::::::::::::::::::::::::::::
And sometimes without "parent" like the following:
::::::::::::::::::::::::::::::::::::::::::::::::
class MyClass2(QWidget):
    def __init__(self, name):
        super(MyClass2, self).__init__()
        print(self.parent().test)
::::::::::::::::::::::::::::::::::::::::::::::::
Could anyone explain this to me when and for which action I have to use the first one "MyClass1" and when the second one "MyClass2"?

Regards
Mohsen


More information about the Python-list mailing list