Sea of Tranquility    About    Archive    Feed

The insignificance of 'self'

Suppose you are defining a function inside a class.

Suppose that the function takes an instance of the class as a parameter.

You name the parameter the way you want. It could be ‘x’, ‘p’, ‘t1’.. whatever. It’s a just a variable.

However, all Python programmers seem to have agreed to use the same variable name. And that name is ‘self’.

self is not a keyword – it’s just a variable name. There is nothing special about the word ‘self’.

If the community had chosen to use a different word, say ‘pizza’, it would have made no difference to the code. It’s just like calling your variable ‘y’ instead of ‘x’.

To understand the insiginificance of ‘self’, replace ‘self’ with any other word and run the program. It doesn’t make any difference.

So, whenever you see word ‘self’ in a program, just remember that it’s just a name assigned to the instance of the object. It has no special meaning.

We tend think that it has a special meaning simply because it appears frequently.