What is the difference between this and self in python?

About self in Python (here is the source: Python self explanation):

Show

The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.) self is not special to the code, it's just another object.

Python could have done something else to distinguish normal names from attributes -- special syntax like Ruby has, or requiring declarations like C++ and Java do, or perhaps something yet more different -- but it didn't. Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self..

About this in Java being explained by Oracle (here is the source: Java this explanation):

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

Last updated on Jul 13,2022 262.8K 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?

What is the difference between this and 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

Course NameDate
Python Certification Training Course

Class Starts on 10th September,2022

10th September

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 24th September,2022

24th September

SAT&SUN (Weekend Batch)
View Details

What is the difference between this and self in python?

Application of Clustering in Data Science Using Real-Time Examples

Watch Now

What is the difference between this and self in python?

Sentiment Analysis In Retail Domain

Watch Now

What is the difference between this and self in python?

Mastering Python : An Excellent tool for Web Scraping and Data Analysis

Watch Now

What is the difference between this and self in python?

Python Tutorial – All You Need To Know In Python Programming

Watch Now

What is the difference between this and self in python?

Web Scraping And Analytics With Python

Watch Now

What is the difference between this and self in python?

Introduction to Business Analytics with R

Watch Now

What is the difference between this and self in python?

Data Science : Make Smarter Business Decisions

Watch Now

What is the difference between this and self in python?

Python Classes – Python Programming Tutorial

Watch Now

What is the difference between this and self in python?

Python Programming – Learn Python Programming From Scratch

Watch Now

What is the difference between this and self in python?

The Whys and Hows of Predictive Modelling-I

Watch Now

What is the difference between this and self in python?

Python List, Tuple, String, Set And Dictonary – Python Sequences

Watch Now

What is the difference between this and self in python?

3 Scenarios Where Predictive Analytics is a Must

Watch Now

What is the difference between this and self in python?

Python Loops – While, For and Nested Loops in Python Programming

Watch Now

What is the difference between this and self in python?

Know The Science Behind Product Recommendation With R Programming

Watch Now

What is the difference between this and self in python?

Python Numpy Tutorial – Arrays In Python

Watch Now

What is the difference between this and self in python?

What is Socket Programming in Python and how to master it?

Read Article

What is the difference between this and self in python?

Statistics for Machine Learning: A Beginner’s Guide

Read Article

What is the difference between this and self in python?

5 Data Science Projects – Data Science Projects For Practice

Read Article

What is the difference between this and self in python?

How to Learn Python 3 from Scratch – A Beginners Guide

Read Article

What is the difference between this and self in python?

Top Data Science Interview Questions For Budding Data Scientists In 2022

Read Article

What is the difference between this and self in python?

Java vs Python : Comparison between the Best Programming Languages

Read Article

What is the difference between this and self in python?

How To Best Utilize Python CGI In Day To Day Coding?

Read Article

What is the difference between this and self in python?

Everything You Need To Know About Print Exception In Python

Read Article

What is the difference between this and self in python?

Learn How To Use Map Function In Python With Examples

Read Article

What is the difference between this and self in python?

How To Become A Python Developer : Learning Path For Python

Read Article

What is the difference between this and self in python?

Python Visual Studio- Learn How To Make Your First Python Program

Read Article

What is the difference between this and self in python?

Data Analyst Roles and Responsibilities : All You Need to Know

Read Article

What is the difference between this and self in python?

Big Data Analytics: BigQuery, Impala, and Drill

Read Article

What is the difference between this and self in python?

What Is Python Programming language | Headstart With Python Basics

Read Article

What is the difference between this and self in python?

How to Implement Membership Operators in Python

Read Article

Is self in Python same as this?

Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.

What is difference between self and self in Python?

There is no difference between (self) and (self,) . The main advantages are that it makes multi-line lists easier to edit and that it reduces clutter in diffs.

What self means Python?

In Python, the word self is the first parameter of methods that represents the instance of the class. Therefore, in order to call attributes and methods of a class, the programmer needs to use self .

Where do we use self in Python?

Use self when: you define an instance method, since it is passed automatically as the first parameter when the method is called; you reference a class or an instance attribute from inside an instance method; you want to refer to instance variables and methods from other instance methods.