[PyQt] Help on "self"

Josh jknox at irobot.com
Thu Feb 11 20:59:47 GMT 2010

David Arnold wrote:
> Hi,
> I'm not new to Python, but the use of "self" in classes has always been confusing. I working thru Rapid Gui Programming, and I have this code from Chatper 5:
> import sys
> from PyQt4.QtGui import *
> class PenPropertiesDlg(QDialog):
>     def __init__(self, parent=None):
>         super(PenPropertiesDlg, self).__init__(parent)
>         widthLabel=QLabel("&Width")
>         self.widthSpinBox=QSpinBox()
> Can someone "gently" explain why the author did not use self.widthLabel=QLabel("&Width").
> Thanks.
> David.
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
The widthLabel doesn't need to be referenced outside the PenPropertiesDlg.
Later in the book example you'll see:
layout.addWidget(widthLabel, 0, 0)

So the label gets created and placed in the layout, but there's no need 
to get at it later.
If it had been created as self.widthLabel = ... then it would be 
accessible via an instance of PenPropertiesDialog. If its not necessary 
it would just clutter the namespace.

Perhaps if the intent was to update the label dynamically, then it could 
be declared with self.widthLabel.

That help at all?


More information about the PyQt mailing list