- Published on
this keyword in JavaScript and self keyword in Python: Any Difference?
- Authors
- Name
- Ibtesum Reza Aninda
- @IbtesumAninda
We use this
in JavaScript and self
in Python. Are they same? This question was haunting me for some time. Finally after some digging I came up with a solution. Hope you will love it.
Personally I love examples. So without further ado, let's jump into some examples.
Here is some JS code with this
:
function Love(partnerName, myName) {
this.partnerName = partnerName;
this.myName = myName;
this.get_married = function () {
return `${this.partnerName} and ${this.myName} got married.`
}
}
const couple1 = new Love('Ross', 'Rachel')
const couple2 = new Love('Chandler', 'Monica')
console.log(couple1.get_married())
console.log(couple2.get_married())
Here is the same code translated to Python:
class Love:
def __init__(self, partnerName, myName):
self.partnerName = partnerName
self.myName = myName
def get_married(self):
return f"{self.partnerName} and {self.myName} got married."
couple1 = Love("Ross", "Rachel")
couple2 = Love("Chandler", "Monica")
print(couple1.get_married())
print(couple2.get_married())
Now, what do you think their output would be? Would there be any difference? Think about it for at least one minute.
So the output for both the JS code and Python code would be:
Ross and Rachel got married.
Chandler and Monica got married.
Now we see that both this
and self
acts similarly. Yes! There is no difference.
But...
I lied, partially. Though their output is the same, this
and self
are not quite same.
this
:
Value of The value of this
depends on the context in which it appears. This context can be a function
, class
or global(Window
for browser, global
for Node.js).
For easier understanding, the value of this
depends on how the function is called.
Note: JavaScript functions can be called in four ways! Such as:
- Calling a function as a function.
- Calling a function as method.
- Calling a function as constructor.
- Calling a function by
call
andapply
self
:
Value of On the other hand, in Python, the value of self
depends on the instance created from the class. In this case, the object created from the couple1
instance, is the value of self
. And couple2
is the value of self
for the second object.
We can prove it by the following code:
class Love:
def __init__(self, partnerName, myName):
self.partnerName = partnerName
self.myName = myName
def get_married(self):
print("id of self: ",id(self)) # Notice this line. It prints the id of "self".
return f"{self.partnerName} and {self.myName} got married."
couple1 = Love("Ross", "Rachel")
couple2 = Love("Chandler", "Monica")
print("id of couple1: ", id(couple1)) # Notice this line. It prints the id of "couple1".
print(couple1.get_married())
print("id of couple2: ", id(couple2)) # Notice this line. It prints the id of "couple2".
print(couple2.get_married())
The output would be:
id of couple1: 140337639399376
id of self: 140337639399376 # This number is the same as the above number.
Ross and Rachel got married.
id of couple1: 140337640683984
id of self: 140337640683984 # This number is the same as the above number.
Chandler and Monica got married.
If you look closely to the ids, you will notice its the same for self
and the object instances. This proves that self
is actually denoting the instances(couple1 and couple2)
Differences:
- First of all,
this
is a reserved keyword.self
is not. You can use anything in lieu ofself
. But it has to be the first parameter. this
is dynamically bound(runtime bound), meaning it changes its behavior based on how the function is called. There are many ways to call a function in JavaScript. On the other hand,self
in Python is statically bound. It means the value ofself
is determined by the instance of theclass
from where the method is called from.
These are the fundamental differences between them. If you have any further query, feel free to comment.
Happy Coding!
-- This article was originally published in DEV Community(dev.to).