What is the use of self in Python?
If you are working with Python, there is no escaping from the word “self”. It is used in method definitions and in variable initialization. The self method is explicitly used every time we define a method. In this article, we will get into the depth of self in Python in the following sequence:
- What is the use of self in Python?
- Python Class self Constructor
- Is self in Python a Keyword?
What is the use of Self in Python?
The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason why we use self is that Python does not use the ‘@’ syntax to refer to instance attributes. In Python, we have methods that make the instance to be passed automatically, but not received automatically.
Example:
class food():
# init method or constructor
def __init__(self, fruit, color):
self.fruit = fruit
self.color = colordef show(self):
print("fruit is", self.fruit)
print("color is", self.color )apple = food("apple", "red")
grapes = food("grapes", "green")apple.show()
grapes.show()
Output:
Fruit is apple
color is red
Fruit is grapes
color is green
Python Class self Constructor
self is also used to refer to a variable field within the class. Let’s take an example and see how it works:
# name made in constructor
def __init__(self, John):
self.name = Johndef get_person_name(self):
return self.name
In the above example, self refers to the name variable of the entire Person class. Here, if we have a variable within a method, self will not work. That variable is simply existent only while that method is running and hence, is local to that method. For defining global fields or the variables of the complete class, we need to define them outside the class methods.
Is self a Keyword?
self is used in different places and often thought to be a keyword. But unlike in C++, self is not a keyword in Python.
self is a parameter in function and the user can use a different parameter name in place of it. Although it is advisable to use self because it increases the readability of code.
Example:
class this_is_class:
def show(in_place_of_self):
print("It is not a keyword "
"and you can use a different keyword")object = this_is_class()
object.show()
Output:
It is not a keyword and you can use a different keyword
It is not a keyword and you can use a different keyword
With this, we have come to the end of our article. I hope you understood the use of self and how it works in Python.
If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.
1. Machine Learning Classifier in Python
2. Python Scikit-Learn Cheat Sheet
4. Python Libraries For Data Science And Machine Learning
9. OOPs Interview Questions and Answers
10. Resume For A Python Developer
11. Exploratory Data Analysis In Python
12. Snake Game With Python’s Turtle Module
14. Principal Component Analysis
15. Python vs C++
16. Scrapy Tutorial
17. Python SciPy
18. Least Squares Regression Method
19. Jupyter Notebook Cheat Sheet
20. Python Basics
23. Python Decorator
25. Mobile Applications Using Kivy In Python
26. Top 10 Best Books To Learn & Practice Python
27. Robot Framework With Python
28. Snake Game in Python using PyGame
29. Django Interview Questions and Answers
30. Top 10 Python Applications
31. Hash Tables and Hashmaps in Python
32. Python 3.8
34. Python Tutorial
Originally published at https://www.edureka.co on October 3, 2019.