0

Why do we use 'self' in python and not 'this', like many other languages do?

Coding languages that support object-orienteted programming usually have a keyword to reference the object by which the method is called. In most of the coding languages I've met so far (C++, Java, Js) this keyword is hardcoded and it is 'this'. In Python, instead, this word can be chosen by the coder, but it is recommended to use the conventional 'self'. As 'self' can be changed, why don't all coders just write 'this' also in Python, so that the word is the same for each language? Is there a reason for that?

18th Aug 2021, 11:28 PM
T3o
1 Antwort
+ 1
As with many things, this is a matter of convention. While languages inspired from C++ use 'this', like Java, C#, or PHP, there are other languages which use 'self', such as Python, Perl, Ruby, Rust, or even Swift. Visual Basic uses 'Me'. So no, not all languages use 'this', just the languages you encountered this far. Hopefully that answered your question (a bit late after 9 months, but still)
5th Jun 2022, 7:56 PM
Amaras A
Amaras A - avatar