What is the use of self in Python - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
What is the use of self in Python?
Consider the following example, which will explain the use of self in full.
Here I'll develop an example that is best suited for Python 3.
Example:
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")?
??
audi.show()???? # same output as car.show(audi)?
ferrari.show()? # same output as car.show(ferrari)?
??
Note:?
Here I'll develop an example that is best suited for Python 3.
Example:
class this_is_class:??
????def show(in_place_of_self):??
????????print("we have used another " "parameter name in place of self")??
??????????
object = this_is_class()??
object.show()?
?Python class self-Constructor:
Python launched version 3.0, sometimes known as Python 3, in December 2008. Technically, this version was created primarily to address serious issues identified in Python 2. As you are aware, Python 3 was incompatible with Python 2 in a number of ways. It is not backward compatible, and certain Python 3 capabilities have been backported to Python 2.x versions to facilitate the transfer to Python 3.
Constructors are widely used to instantiate objects.
It is often used to initialize (assign values to) the data members of a class.
When a class object is created, its members are initialized by the constructor.
领英推荐
In Python, the init() method is known as the constructor, and it is always called when an object is created.
Consider the following example, which demonstrates how the zip function is applied in Python3.
Syntax:
def init(self):
????# body of the constructor
Example:
class Person:?
??
????# default constructor?
????def init(self):?
????????self.name = "Smith"
??
????# a method for printing data members?
????def print_name(self):?
????????print(self.name)?
??
??
# creating object of the class?
obj = Smith()?
??
# calling the instance method using the object obj?
obj.print_name()
Is self in Python a Keyword?
As previously stated, the self is mostly utilized in various contexts as an argument to a function. But it's not a keyword.
Scope @ NareshIT:
At Naresh IT, you will discover an experienced faculty that will guide, advise, and nurture you as you strive toward your goals.
Here, you will receive excellent hands-on experience in a real-world industry environment, which will surely help you plan your future.
During the application design process, we will also educate you about various features of the application.
Our skilled trainer will walk you through the entire problem situation.
Our motto is "Achieve Your Dream Goal." Our fantastic team works diligently to guarantee that our students click on their goals. So, believe in us and our advice, and we assure you success.
FAQ'S
1. What is the purpose of the self keyword in Python class methods?
The self keyword in Python is used to reference the current instance of a class. It allows you to access and modify the instance's attributes and methods within the class definition.
2. Why is self used as the first parameter in class methods?
When you call an instance method, Python automatically passes the instance itself as the first argument to the method. This argument is conventionally named self. Using self allows the method to access and manipulate the specific instance's attributes and methods.
3. Can I use a different name for the self parameter?
While self is the most common convention, you can technically use any valid variable name as the first parameter in class methods. However, it's strongly recommended to stick with self for consistency and readability.
For More Details Visit : Python Online Training
Register For Free Demo on UpComing Batches : https://nareshit.com/new-batches