So i've been learning PyQt5 recently and i've noticed that my tutor sometimes uses "self" before the name of the instance he makes, and sometimes does not. both seem to work:
Example code: (adding a simple push botton to our mainwindow, here we've used self.btn1 = QPushBottun("button1", self)
import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton,QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.btn1 = QPushButton('Button1',self)
self.btn1.move(30,50)
self.setGeometry(300,300,290,150)
self.setWindowTitle('Event Sender')
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
And here in the code below we just say: btn1 = QPushBottun("button1",self)
import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton,QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton('Button1', self)
btn1.move(30,50)
self.setGeometry(300,300,290,150)
self.setWindowTitle('Event Sender')
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
both of these work. My question is that in what case we use "self" and in what cases we don't?