Menu Close

Python self parameter

In this article, we are going to learn all about Python self parameter along with examples. If you are working with Object-Oriented Programming in Python, then you can’t ignore self parameters in Python.

In previous tutorials, we have discussed the following Object Oriented topics that are very helpful for you.

Let’s try to understand self parameter in Python along with examples.

Python self parameter

Python self parameter represents the current instance or object of the class. By using a self parameter, you can access properties and methods which lie inside the Python class.

When we defined a method for a class, Then self is used as the first parameter for each method, You can change the name of the self parameter as your choice but it should be first.

Instance Method

The instance method in Python is the normal method that mostly used in python programming. The instance method in Python is bound to the class that is used to access the unique data of the instances or objects. Python instance method call using object or instance of the class.

Python instance method accepts self as the first parameter but is can be changed.

Example

class Students:
	
	#constructor that assign values inside instance variables.
	def __init__(self, name, age, course):
		self.st_name = name
		self.st_age = age
		self.st_course = course

	#instance method that print student details
	def printDetails(self):
		print(f"My name is {self.st_name} and I am {self.st_age} years old currently i am pursuing {self.st_course}.")

#Create two objects of Student class.
s1 = Students("Vishvajit Rao", 23, "MCA")
s2 = Students("Vinay", 20, "BA")

#call the instance method using object.
s1.printDetails()
s2.printDetails()

Output

My name is Vishvajit Rao and I am 23 years old currently i am pursuing MCA.
My name is Vinay and I am 20 years old currently i am pursuing BA.

In above exmaple, we have created a class Students which constains.

  • __init__ constructor which is called during the object or instance creation of the class and assigns all the values inside st_name, st_age, and st_course instance variables.
  • printDetails() instance method that is capable to give unique information about instances (s1 and s2).
  • Both __init__ and printDetails() accepts self and first argument, that represent the objects (s1 and s2) of the class.

Change Python self parameter

Sometimes you want to change the name of self parameter, Then you can change it but should be first.

Example

class Students:
	
	#constructor that assign values inside instance variables.
	def __init__(obj, name, age, course):
		obj.st_name = name
		obj.st_age = age
		obj.st_course = course

	#instance method that print student details
	def printDetails(obj):
		print(f"My name is {obj.st_name} and I am {obj.st_age} years old currently i am pursuing {obj.st_course}.")


#Create two objects of Student class.
s1 = Students("Vijay", 20, "BSc")
s2 = Students("Vijya", 24, "MSc")

#call the instance method using object.
s1.printDetails()
s2.printDetails()

Output

My name is Vijay and I am 20 years old currently i am pursuing BSc.
My name is Vijya and I am 24 years old currently i am pursuing MSc.

As you can see in above example, we have replaced self to obj and rest of the concepts same as above example.

Conclusion

In this article, you have learned all about Python self parameter along with examples. the self parameter in Python basically used to access the value of attributes and methods which are lie inside the class.
Without self parameter, you can not access the value of attributes and methods.

I hope you don’t have any confusion regarding the Python self parameter. If this article helpful for you, please share and keep visiting for further Python tutorials.

For more information:- Click Here

Python setattr() function
Python sys module

Related Posts