Last updated on Jul 13,2022 267.3K Views
3 / 62 Blog from Python Fundamentals
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. Join our Master Python programming course to know more. 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 = color def 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:
class Person: # name made in constructor def __init__[self, John]: self.name = John def 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.
Find out our Python Training in Top Cities/Countries
India | USA | Other Cities/Countries |
Bangalore | New York | UK |
Hyderabad | Chicago | London |
Delhi | Atlanta | Canada |
Chennai | Houston | Toronto |
Mumbai | Los Angeles | Australia |
Pune | Boston | UAE |
Kolkata | Miami | Dubai |
Ahmedabad | San Francisco | Philippines |
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
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.
Check out the Python Certification course by Edureka. This Training course is designed for students and professionals who want to be a Python Programmer. The course is designed to give you a head start into Python programming and train you for both core and advanced concepts.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible or join our Python Training in Chennai Today..
Upcoming Batches For Python Certification Training Course
Python Certification Training Course | Class Starts on 15th October,2022 15th October SAT&SUN [Weekend Batch] | View Details |
Python Certification Training Course | Class Starts on 12th November,2022 12th November SAT&SUN [Weekend Batch] | View Details |
Recommended videos for you
Introduction to Business Analytics with R
Watch Now
Web Scraping And Analytics With Python
Watch Now
Application of Clustering in Data Science Using Real-Time Examples
Watch Now
The Whys and Hows of Predictive Modeling-II
Watch Now
Data Science : Make Smarter Business Decisions
Watch Now
3 Scenarios Where Predictive Analytics is a Must
Watch Now
Python Loops – While, For and Nested Loops in Python Programming
Watch Now
Python List, Tuple, String, Set And Dictonary – Python Sequences
Watch Now
Python Programming – Learn Python Programming From Scratch
Watch Now
Python Tutorial – All You Need To Know In Python Programming
Watch Now
Sentiment Analysis In Retail Domain
Watch Now
Python Classes – Python Programming Tutorial
Watch Now
Know The Science Behind Product Recommendation With R Programming
Watch Now
Business Analytics Decision Tree in R
Watch Now
Python Numpy Tutorial – Arrays In Python
Watch Now
Mastering Python : An Excellent tool for Web Scraping and Data Analysis
Watch Now
Recommended blogs for you
Difference Between Data Scientist and Data Analyst
Read Article
What is Overfitting In Machine Learning And How To Avoid It?
Read Article
Install Python On Windows – Python 3.X Installation Guide
Read Article
R Tutorial – A Beginner’s Guide to Learn R Programming
Read Article
Data Scientist Resume Sample – How To Build An Impressive Data Scientist Resume
Read Article
OpenCV Python Tutorial: Computer Vision With OpenCV In Python
Read Article
Python Pandas Tutorial : Learn Pandas for Data Analysis
Read Article
How to Implement Optical Character Recognition in Python
Read Article
What is Mutithreading in Python and How to Achieve it?
Read Article
Object Oriented Programming Python: All you need to know
Read Article
How To Implement Linear Discriminant Analysis in R?
Read Article
What is Supervised Learning and its different types?
Read Article
Cluster Analysis Steps in Business Analytics with R
Read Article
Top Reasons Why Business Analytics Is A Must For MBA Grads!
Read Article