What is purpose of 'self' in python class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

What is purpose of 'self' in python class?

What is the purpose of the self word in Python? I understand it refers to the specific object created from that class, but I can't see why it explicitly needs to be added to every function as a parameter? Can anyone talk me through this?

6th Mar 2018, 12:46 AM
Ishmam Hasnat Iram
Ishmam Hasnat Iram - avatar
28 Answers
+ 18
You need to add "self" as the first argument of every class method because "self" is a reference to the current instance of the class, and you'll know why explicit "self" has to stay in python by Guido van rossum http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html
6th Mar 2018, 1:48 AM
ƒred
ƒred - avatar
+ 17
From my understanding of OOP in Python, self is basically just a placeholder for the name of the class when you're referring to it outside of the class itself. For example: class Test: def __init__(self, x, y): self.hi = x self.hello = y z = Test(5, 6) print(z.hi) print(z.hello) #In the class, self just represents the name of the variable that created the class, in this case being z
6th Mar 2018, 12:55 AM
Faisal
Faisal - avatar
+ 10
It is similar to "this" pointer in c++. If you are familiar with c++, then you can relate them.
7th Mar 2018, 1:38 AM
Hariharasubramanian
+ 8
self is a scope variable which refers to the current instance of the class allowing you to access it's properties and methods.
7th Mar 2018, 1:31 PM
Nathaniel Blackburn
Nathaniel Blackburn - avatar
+ 7
if you ask me , I think it just a syntax thing because other languages don't require you to use it to indicate that ur referencing the current object but in some cases u do need it. take for example JavaScript, in JavaScript u don't use the word "this" when declaring methods but you do have to use it when declaring a variable. so really it's a matter of design. The less you have to write or worry about the higher level the language is. the more you write the lower than ge is.
7th Mar 2018, 4:18 PM
jay
+ 7
'Self' is an identifier that the function uses to recognize an object within the class's attributes. It's a way to access/call the attributes of the object created within the class. For example: class Company: def __init__(self): #Constructor using 'self' to initialize starting values of the object being created self._nameOfCompany = "" self._salesAmt = 0.0 def setName(self,argument): #'self' in this function is used to call and change the attribute 'nameOfCompany' self._nameOfCompany = argument def weirdFunction(): #if for some reason, there's a function within the class that doesn't need access to the attributes of the object, you don't need to put in 'self'. 2 == 2
10th Mar 2018, 2:50 PM
PoofPillow
PoofPillow - avatar
+ 6
used mostly for accessing constructor varaiables
6th Mar 2018, 1:42 PM
gnaneswari kolathuru
gnaneswari kolathuru - avatar
+ 6
self is a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
6th Mar 2018, 2:21 PM
Somasundaram R
Somasundaram R - avatar
+ 6
A represenation of the object you will create of that class
11th Mar 2018, 12:56 PM
Ashraf Al_Absi
Ashraf Al_Absi - avatar
+ 5
and just to complete what the others said, u can use any other word insteed of self but self is agreed to be used form the world developers communiy
6th Mar 2018, 5:00 PM
fatiz
+ 5
ive understood it from the comments
8th Mar 2018, 5:51 PM
Android Boi
Android Boi - avatar
+ 5
The "self" parameter is an object, and its purpose is to call the method of the class. For example, in the code below: class Cat: def __init__(self, color): self.color = color the class is "Cat" and the method is "color". So, for any animal cat , you have the corresponding color. It's mandatory !!!
10th Mar 2018, 10:01 PM
Tanoh Le Kadiographe
Tanoh Le Kadiographe - avatar
+ 4
thanks everyone here for helping me out
8th Mar 2018, 9:31 PM
Ishmam Hasnat Iram
Ishmam Hasnat Iram - avatar
+ 4
It’s just the conventional way of doing it. At least that’s what I was taught.
11th Mar 2018, 6:01 PM
Jimoh Ibraheem
Jimoh Ibraheem - avatar
+ 4
Thanks Paavan Gupta, Ur explanation is really helpful 👍👌❤️
12th Mar 2018, 3:23 PM
Ishmam Hasnat Iram
Ishmam Hasnat Iram - avatar
+ 3
From my understanding of OOP in Python, self is basically just a placeholder for the name of the class when you're referring to it outside of the class itself. For example: class Test: def __init__(self, x, y): self.hi = x self.hello = y z = Test(5, 6) print(z.hi) print(z.hello) #In the class, self just represents the name of the variable that created the class, in this case being z
7th Mar 2018, 5:01 PM
dieserrapha
dieserrapha - avatar
1st May 2018, 9:31 PM
Johannes
Johannes - avatar
+ 2
self is used to refer an object of a class even before the object is created. if you know c++ or javascript then you may have learned about the 'this' pointer. the same work is of the self keyword in python. for an example, if you want to make a class of employees then you may need to increment the salary of an employee certain times. now, if the employee object is not yet created then how can you increase the salary of that particular employee. here you use the self keyword. class employee: def __init__(self, salary): self.salary = salary def increamentSalary (self, increase): self.salary = self.salary + increase
12th Mar 2018, 3:05 PM
Paavan Gupta
Paavan Gupta - avatar
+ 2
what feature is unique in python that cannot be found in other computer languages?
12th Mar 2018, 7:49 PM
Moses Nyamai
Moses Nyamai - avatar
+ 2
@Moses_Nyamai (Features of Python): 1. Python is easy to learn because of easy syntex. So you don't have to memorise hard syntex that other languages have. Instead you can learn the actual programming 2. Python has a huge library for data analysing, web crawling, back-nd programming etc. So it's fun to play with them 3. Many good websites like Google, Dropbox, Instagram, Twitter uses python hugely 4. And of course for Beginner's learning Python is no doubt the best
14th Mar 2018, 6:07 PM
Ishmam Hasnat Iram
Ishmam Hasnat Iram - avatar