0

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sherafati
  • 196
  • 1
  • 10
  • 1
    If you ever want to access the object outside of the current method, you can assign it to the instance (making it an instance attribute). If the object only needs to be known within the current scope, then you don't have to assign it to the instance. So ask yourself: are you planing to use ``self.btn1`` or ``ex.btn1`` again, or not. – Mike Scotty May 20 '20 at 08:23
  • @MikeScotty So although we are still in the same class, if we want to use that object inside another method of the same class we will have to assign it to our instance (self). Also if we want to create a new attribute for our instance it has to be done inside our `__init__` method, correct? – Sherafati May 20 '20 at 08:29
  • 1
    Yes, ``btn1`` is a local variable that's only known in that method's scope (read up on scopes), while ``self.btn1`` is an instance attribute and is as such accessible in all instance methods once it is set. And **no**, you may add new attributes to existing objects whenever you feel like it, that's what Python allows you to do. But you can call it a convention to set/initialize instance attributes in the ``__init__`` method, so others that want to look them up can easily find them (see [POLA](https://en.wikipedia.org/wiki/Principle_of_least_astonishment)). – Mike Scotty May 20 '20 at 13:17
  • 1
    Like I wrote, it exists once it is set. You cannot access it before it is set. Thus the convention to set attributes in ``__init__``, because it's usually the first instance method to be executed. Otherwise it's like writing ``print(a); a=5``. You cannot acces what's not defined yet. – Mike Scotty May 20 '20 at 13:32

0 Answers0