Feb-07-2017, 11:43 AM
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:
Confused
iFunc
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