"this" in JavaScript and "self" in Python; Any Difference?
Tags: javascript, python, programming
Published: 2023-05-21 00:01:29

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:
```js
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:
```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: 
```bash
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. 


## Value of `this`:

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` and `apply`


## Value of `self`:
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:
```python
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: 
```bash
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: 
1. First of all, `this` is a **reserved** keyword. `self` is not. You can use anything in lieu of `self`. But it has to be the first parameter.
2. `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 of `self` is determined by the instance of the `class` 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!