Python Forum
Why is there an __init__ statement within the __init__ definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is there an __init__ statement within the __init__ definition
#1
Hi there,

could someone please explain why with this bit of code I have copied, there is a second init statement in the init definition:
#!/usr/bin/python3

import sys
from PyQt4 improt QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init___(self, parent=None):
        QtGui.QWidget.__init__(self, parent)                        # <--  this line of code, what does it do???
        self.setGeometry(0, 0, 1920, 1080)
        self.home()

    def  home(self):
        self.lbl = QtGui.QLabel("Hello World!", self)
        self.lbl.setGeometry(QtCore.QRect(800, 30, 1161, 101))
        self.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = Window()
    sys.exit(app.exec_))
So this seems to be the complete minimum code required in order to get a window and a label using object oriented programming. I also don't understand why I am having to call the home method from the init method, that doesn't make sense either, but if I take that out, then the code runs without error, but there is no window, and no way to stop the script either. If someone could help explain this that would really help.

I'm really struggling with OOP, and have been for about 6 months (currently I am being paid to learn to program, so when I say 6 months, that's 8 hours a day for six months) to the point I can only copy code, I can't write my own, is this normal?
Reply
#2
Please, amend the thread title to something meaningful.
Reply
#3
(Feb-06-2017, 12:12 PM)iFunKtion Wrote: 8 hours a day for six months
...
I can only copy code, I can't write my own, is this normal?

I don't think it is. Seems like you are trying to understand advanced programs while missing some essential basics.

Of course you can program-by-copying-others-codes, but while the result may "work", that won't improve much your understanding.

Which OOP tutorial do you use?
Reply
#4
I have been following mainly this fellow: https://pythonprogramming.net/

But as he himself points out he doesn't really use OOP which makes life tricky. I have not found any tutorials on OOP that I have been able to fully understand or make sense of and then transferring that over to the concept of a GUI has just thrown me completely.
Reply
#5
The inner __init__ is calling the Base class __init__.
You are creating your own version of the Base class and adding things to it. You still have to call the Base class methods to get those things to happen in the same way you would if you had just made a direct instance of the Base class.
Your new __init__ overwrites the Base class __init__ so you have to call the Base class __init__ yourself.
Reply
#6
Yes, I think that makes some sort of sense. So to clarify, I am calling this because I am using the another library, in this case Qt4, in other words, if I were just creating a basic program, not a GUI where I was just creating my own classes out of the basic python, I would be defining an init definition for the class, but the inner init would not be there as I would not be trying to inherit from anything else?
Reply
#7
So for anyone else that may end up here looking for clarification, Mr Yoriz helped along the way, and here I found some clarification of what was explained:
http://zetcode.com/gui/pyqt4/firstprograms/

Specifically this quote:
Quote:The three most important things in object oriented programming are classes, data, and methods. Here we create a new class called Example. The Example class inherits from QtGui.QWidget class. This means that we call two constructors: the first one for the Example class and the second one for the inherited class. The super() method returns the parent object of the Example class and we call its constructor. The __init__() method is a constructor method in Python.
Reply
#8
Quote:So this seems to be the complete minimum code required in order to get a window and a label using object oriented programming

Not so:

try this:
import tkinter as tk

class ziggy:
    def __init__(self):
        w = tk.Tk()
        l = tk.Label(w, text='I am a label').pack()
        w.mainloop()

ziggy()
doesn't use Qt, but has a graphics window, with label, and is object oriented
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 236 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  __init__() got multiple values for argument 'schema' dawid294 4 2,281 Jan-03-2024, 09:42 AM
Last Post: buran
  mutable argument in function definition akbarza 1 471 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  error occuring in definition a class akbarza 3 690 Nov-26-2023, 09:28 AM
Last Post: Yoriz
  determine parameter type in definition function akbarza 1 585 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Initiating an attribute in a class __init__: question billykid999 8 1,314 May-02-2023, 09:09 PM
Last Post: billykid999
Question __init__ of Child Class zero_fX0 4 1,687 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 739 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  Explain the python code in this definition Led_Zeppelin 1 1,090 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 1,950 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020