Whenever object-oriented programming is done in Python, then we will often come across the self method as their first parameter. This article explains the main concept of self method in Python for Windows development.
self represents the instance of the class in Python. By using the “self” keyword we can access the attributes and methods of the class in Python. It binds the attributes with the given arguments.
The reason you need to use self because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: The first parameter of methods is the instance the method is called on.
Here is the basic example of how we can use self keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Cat: def __init__(self, name, age): self.name = name self.age = age def info(self): print(f"I am a cat. My name is {self.name}. I am {self.age} years old.") def make_sound(self): print("Meow") Cat("Kitty", "4").info() Cat("Kitty", "4").make_sound() |
The output in PyScripter IDE:
Here is another example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class car(): # __init__ method or constructor def __init__(self, model, color): self.model = model self.color = color def show(self): print("Model is", self.model ) print("color is", self.color ) # Both objects have different self which contain their attributes audi = car("audi a4", "blue") ferrari = car("ferrari 488", "green") # Same output as car.show(audi) audi.show() # Same output as car.show(ferrari) ferrari.show() # Behind the scene, in every instance method call, Python sends the instances also with that method call like car.show(audi) |
The output in PyScripter IDE:
Note: Self is a convention and not a real Python keyword. self is a parameter in function and the user can use another parameter name in place of it. But, it is advisable to use self to increase our code readability.
The following code is the working example of using self parameter in the context of creating GUI using DelphiVCL library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from DelphiVCL import * class MainForm(Form): def __init__(self, Owner): self.Caption = "A VCL Form..." self.SetBounds(10, 10, 340, 410) self.lblHello = Label(self) self.lblHello.SetProps(Parent=self, Caption="Hello Python") self.lblHello.SetBounds(10,10,100,24) self.edit1 = Edit(self) self.edit1.SetProps(Parent=self, Top=30, Left=10, Width=200, Height=24) self.button1 = Button(self) self.button1.SetProps(Parent=self, Caption="Add", OnClick=self.Button1Click) self.button1.SetBounds(220,29,90,24) self.lb1 = ListBox(self) self.lb1.SetProps(Parent=self) self.lb1.SetBounds(10,60,300,300) def Button1Click(self, Sender): self.lb1.Items.Add(self.edit1.Text) def main(): Application.Initialize() Application.Title = "My DelphiVCL App" f = MainForm(Application) f.Show() FreeConsole() Application.Run() main() |
And here is the GUI result:
You can browse more complex demo here.
Check out DelphiVCL which easily allows you to build GUIs for Windows using Python.