Understanding the self parameter in Python
The first parameter of the python method is an instance,the method is called on. It refers to a specific object created from that class.self represents the instance of the class. By using the self, we can access the attributes and methods of the class in python.Usually this first method parameter is named as self but this name can be anything.
Below is how we define a method in python class and how do we access the method using an object of the class and to check if the initialization is successful
class Employee: def employeeDetails(self,name): self.name = name employee = Employee() employee.employeeDetails("myName")
print("Name: ",employee.name) Name: myName
Let's add one more method to display welcome message for an employee and try accessing it, since as we see, we do not need a self parameter in the welcomeMessage() method, we didn't used it, now let's try to access it using object
we can see are getting an above error, it is saying that welcomeMessage() takes 0 positional arguments but 1 was given, although we didn't pass any argument while calling a method, why is so?
To understand this we need to see how does python handles the above method call(employee.welcomeMessage()) internally. Although we are calling it in employee.welcomeMessage() this way,python interpreter converts it into Employee.welcomeMessage(employee) (ClassName.method(object)) and this is the reason we were getting an error that you have passed 1 argument when method expects 0. Let's change the method definition and add a self parameter which need not to be self only you can mention any names but since it refers to object it self most commonly it is named as self which makes the code readability more flexible.
class Employee: def employeeDetails(self,name): self.name = "abc" def welcomeMessage(self): print("Welcome to an Organization")
when we call it using employee.welcomeMessage() or Employee.welcomeMessage(employee) below is an output it works perfectly fine.
Now since we know use of self parameter,let's see what if we create an attribute in method without using self parameter
class Employee: def employeeDetails(self,name): self.name = name age = 25 print("Age:",age) def printDetails(self): print("Name: ",self.name) print("age ",age)
when you create an instance attribute moto is to have that attribute throughout a lifespan of that object and can be accessible in all method. If we call
printDetails() we get an below error, which means that age attribute becomes a local for method employeeDetails() and can not access in another method even if we call using the same object.
As we see, we do not need a self parameter in welcomeMessage() why should we use it. In this case we need to make such method as static method using python decorator
class Employee: def employeeDetails(self,name):
self.name = name @staticmethod def welcomeMessage(): print("Welcome to an Organization")
Python decorators are the functions which takes another function and extends their functionality. In above case @staticmethod is the decorator which takes the welcomeMessage() function and extends the functionality such that it ignores the binding of an object where python interpreter understands that it won't need a self parameter and thet it executes it successfully
--
4 年well written Vipul Bramhankar !!
Independent Consultant. DM me for your data engineering needs as well as anything DevOps related.
4 年Well written ....Excellent choice of topic. I'll bet there are scores of python programmers out there who use self daily without really understanding it. This will help them understand.
Data Engineer at Atgeir Solutions
4 年Thanks for sharing this essential topic Vipul Bramhankar?Sir!
Co-Founder and CEO at Atgeir Solutions
4 年Great post Vipul Bramhankar?!! Thanks for sharing.?