Python Forum
Is self supposed to be here or not?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is self supposed to be here or not?
#1
Hi,

Sorry for yet another self question, but this is really unclear, some people seem to use it more than others and I don't know why that is. So here is the code I am learning with, some of it is not relevent as I am trying to learn about classes, but this question of self has cropped up and this code works currently. Here is the code I am learning with:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
Qt4 tutorial using classes

This example will be built
on over time.
"""

import sys
from PyQt4 import QtGui, QtCore


class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 800, 600)
        self.home()
          
    def home(self):
#=================This part of the code works with or without self================#
        self.lbl = QtGui.QLabel("special", self)
        self.lbl.setGeometry(QtCore.QRect(80, 30, 1161, 101))
        self.msgbtn = QtGui.QPushButton("Click Me", self)
        self.msgbtn.setGeometry(QtCore.QRect(130, 280, 130, 26))
        self.msgbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
#==================================================================================#
        self.show()

        
class Form(Window):
    def __init__(self):
        QtGui.QWidget.__init__(self, parent)
           

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = Window()
    sys.exit(app.exec_())
So the part of the code I have marked, all those lines works with or without self. in front of it, and it is very unclear which I should be using. I initially thought yes, but I have done quite a few tutorials and they don't put it there, what should it be and why please? Is it because it is a method and is implied within it but then explicit is better than implicit, so it should be there? that said, I think I have just ruled that out as either all lines require self or none of them. I can only assume, that in this example, self is not necessarily wanted here, as there is only one home page if you like that is imediately being called from the init function and all the objects within that particular class are only used in that instance.

Confused
iFunc
Reply
#2
not ... Here's how it should be written:
    def home(self):
        lbl = QtGui.QLabel("special")
        lbl.setGeometry(QtCore.QRect(80, 30, 1161, 101))
        msgbtn = QtGui.QPushButton("Click Me")
        msgbtn.setGeometry(QtCore.QRect(130, 280, 130, 26))
        msgbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        self.show()
I haven't tried it, but think I got it ok
Reply
#3
It's all down to the need to access things elsewhere in your code.
If that's not needed self is not necessary and you have a variable that's local to the method, but if you need access from another method by adding self it becomes a instance variable and can be accessed again using self.
Reply
#4
That is the Perfect answer Yoriz, that just put masses of this into perspective for me, thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] What is $DISPLAY supposed to be? amethyst 2 2,468 Jun-29-2019, 04:36 PM
Last Post: amethyst
  First Gui attempt and file not working like it is supposed to. Tuck12173 2 6,395 Jan-28-2018, 04:33 AM
Last Post: Tuck12173

Forum Jump:

User Panel Messages

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