+ 1

What is self parameter in python ?

22nd Dec 2021, 6:59 AM
Muhammad Ayaz
Muhammad Ayaz - avatar
3 Answers
+ 1
self represents the instance of the class. 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. Source: https://www.geeksforgeeks.org/self-in-python-class/
22nd Dec 2021, 7:07 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
An example of self parameter would look like this Class Person def __init__(self, n): self.name = n;
22nd Dec 2021, 7:14 AM
Steve Nova
Steve Nova - avatar
+ 1
Eg. class User: def __init__(self, name, age): #called when initializing the class self.Name = name self.Age = age #here, self refers to the current class (object) which is User, so we're setting Name and Age attributes from parameters def say_hi(self): print(self.Name, "says: Hi!") me = User("username", 99) #here me is a User class with attributes: Name="username" and Age=99 me.say_hi() #username says: Hi!
22nd Dec 2021, 9:13 AM
Sousou
Sousou - avatar