Why do pythonistas call the current reference "self" and not "this"?

Python

Python Problem Overview


Python is the language I know the most, and strangely I still don't know why I'm typing "self" and not "this" like in Java or PHP.

I know that Python is older than Java, but I can't figure out where does this come from. Especially since you can use any name instead of "self": the program will work fine.

So where does this convention come from?

Python Solutions


Solution 1 - Python

Smalltalk-80, released by Xerox in 1980, used self. Objective-C (early 1980s) layers Smalltalk features over C, so it uses self too. Modula-3 (1988), Python (late 1980s), and Ruby (mid 1990s) also follow this tradition.

C++, also dating from the early 1980s, chose this instead of self. Since Java was designed to be familiar to C/C++ developers, it uses this too.

Smalltalk uses the metaphor of objects sending messages to each other, so "self" just indicates that the object is sending a message to itself.

Solution 2 - Python

Check the history of Python for user defined classes:

> Instead, one simply defines a function whose first argument corresponds to the instance, which by convention is named "self." For example:

def spam(self,y):
    print self.x, y

> This approach resembles something I > had seen in Modula-3, which had > already provided me with the syntax > for import and exception handling.

It's a choice as good as any other. You might ask why C++, Java, and C# chose "this" just as easily.

Solution 3 - Python

Smalltalk, which predates Java of course.

Solution 4 - Python

With respect to python, there is nothing special about self. You can use this instead if you wanted:

Here's an example:

>>> class A(object):
...    def __init__(this):
...       this.x = 3
... 
>>> a = A()
>>> a.x
3

Although you could name it whatever you want, self is the convention for the first argument of a class function. Check out paragraph 5 of section 9.4 in the python documentation, which says:

> Often, the first argument of a method is called self. This is nothing > more than a convention: the name self has absolutely no special > meaning to Python. Note, however, that by not following the convention > your code may be less readable to other Python programmers, and it is > also conceivable that a class browser program might be written that > relies upon such a convention.

As for the convention, it started out in Smalltalk, but is also used in Object Pascal, Python, Ruby, and Objective-C. This answer has a great explanation.

Solution 5 - Python

Python follows Smalltalk's footsteps in the aspect - self is used in Smalltalk as well. I guess the real question should be 'why did Bjarne decide to use this in C++'...

Solution 6 - Python

The primary inspiration was Modula-3, which Guido was introduced to at DEC:

> the Modula-3 final report was being > written there at about the same time. > What I learned there showed up in > Python's exception handling, modules, > and the fact that methods explicitly > contain “self” in their parameter > list.

-- Guido, Linux Journal Interviews Guido van Rossum

Solution 7 - Python

I think that since it's explicitly declared it makes more sense seeing an actual argument called "self" rather than "this". From the grammatical point of view at least, "self" is not as context dependent as "this".

I don't know if I made myself clear enough, but anyway this is just a subjective appreciation.

Solution 8 - Python

self is not a keyword (*).

self represents by convention the address of the current object

You can get more info on self here.

Why not this ? well it is a convention for a name. You can use this for your code if you like it better.


(*) This answer has been ported and merged here from a question asking why 'self' instead of 'this' keyword. As the clarification in this first line could be useful for others I keep it here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questione-satisView Question on Stackoverflow
Solution 1 - PythonJim FerransView Answer on Stackoverflow
Solution 2 - PythonduffymoView Answer on Stackoverflow
Solution 3 - PythontragomaskhalosView Answer on Stackoverflow
Solution 4 - PythonjterraceView Answer on Stackoverflow
Solution 5 - PythonzmbqView Answer on Stackoverflow
Solution 6 - PythoncdlearyView Answer on Stackoverflow
Solution 7 - PythonfortranView Answer on Stackoverflow
Solution 8 - PythonjoaquinView Answer on Stackoverflow