54

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?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Bite code
  • 578,959
  • 113
  • 301
  • 329

8 Answers8

79

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.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
  • 10
    Object Pascal (both the early Apple and the later Borland flavours, including Delphi) also went with "self". – Paul-Jan Jul 03 '09 at 17:44
  • 7
    Smalltalk and Objective-C both use the metaphor of objects sending messages to each other, so "self" just indicates that the object is sending a message to itself. (It's always seemed more natural to me to use "self" instead of "this".) – Jim Ferrans Jul 03 '09 at 23:49
  • 4
    Not to forget self, the language :) – akuhn Aug 26 '09 at 13:41
  • 4
    Great answer except for conflating C and C++ familiarity when talking about java. Most people doing OOP in C, such as with [gobject](http://developer.gnome.org/gobject/unstable/) use `self` specifically to avoid reserved keyword clashes if their code is ever included in a C++ project. – Karl Bielefeldt May 14 '11 at 06:42
  • 1
    @Karl: Thanks, didn't know that. Agree with you about my somewhat confusing last sentence. – Jim Ferrans May 14 '11 at 17:53
  • There is a notable difference: "this" is a reserved keyword in C/Java and carries a meaning. "self" is not a reserved keyword in Python. – netrox Oct 19 '19 at 23:00
31

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    Sorry mate, but really you are just repeating what I said in the question : python uses self, it's a convention. The question is why. – Bite code Jul 03 '09 at 16:21
  • 7
    The quotes and link explain the creator of Python's motivation. It would be difficult to get a better 'why' than that. Duffymo isn't repeating your question - Guido is. – Joel Hooks Jul 03 '09 at 16:26
  • 5
    "...something I had seen in Modula-3..." - it's got more documentation than "Smalltalk because I said so." I believe the "I" in the history is BDFL Guido. – duffymo Jul 03 '09 at 16:26
  • Didn't follow the link. My mistake. – Bite code Jul 03 '09 at 17:52
  • "why C++, Java, and C# chose 'this' " -> C# chose 'this' because it derives from Java. Java chose 'this' because it derives from C++. C++ chose 'this' because... I don't know:-) – Matthieu Feb 04 '15 at 08:39
  • We'll have to ask Stroustrup why he did it that way. Question is almost 5.5 years old. How does this add value? – duffymo Feb 04 '15 at 10:28
19

Smalltalk, which predates Java of course.

tragomaskhalos
  • 2,733
  • 2
  • 17
  • 10
19

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.

Community
  • 1
  • 1
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • Thanks @jterrace, i was not aware that i can use `this` in python also. Also your description is reasonable. – Aamir Rind Dec 23 '11 at 20:10
  • 1
    Merged this answer into previous, largely-identical question - you may want to give it a quick once-over. – Shog9 Dec 23 '11 at 23:34
  • 1
    "Your code may be less readable to other Python programmers" but it may be *more readable by all the other C#, C++, and Java programmers*, and it may lighten the load on you, if you must program in C++ and Python. – Chris Jan 27 '21 at 22:47
  • Good info, I thought I was forgetting something by not adding/using "self". – Apache Jan 17 '22 at 21:00
8

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++'...

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Merged this answer into previous, largely-identical question - you may want to give it a quick once-over. – Shog9 Dec 23 '11 at 23:34
7

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

cdleary
  • 69,512
  • 53
  • 163
  • 191
6

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.

fortran
  • 74,053
  • 25
  • 135
  • 175
2

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.

Community
  • 1
  • 1
joaquin
  • 82,968
  • 29
  • 138
  • 152
  • Merged this answer into previous, largely-identical question - you may want to give it a quick once-over. – Shog9 Dec 23 '11 at 23:34