what is self?

Michael Chermside mcherm at mcherm.com
Wed Jul 2 18:55:29 EDT 2003


paul h writes:
   [...]
> looking into the code, i see a lot of references to "self" ie
> self.window = ...
   [...]

In Python, "self" is nothing special at all... it's just a normal
variable like "x" or "y" or "myObject". If you look at the top of
the method declaration where you saw self used, you'll see that
"self" is the name of the first argument.

What's different now is that you're starting to use objects. When
you use objects in Python, they have functions that belong to the
objects themselves (called "methods"). In Python, the first
argument to any method (a function defined within a class) will
always be the particular object on which the method is being
called. Since that first argument always means "the object this
method is called on" Python programmers nearly always use the
same name for it... and "self" happens to be the conventional name.

Everything would work just fine if you changed "self" to "abc" in
the parameter list and also within the body of the method. 
Everything, that is, except for the fact that people reading your
code would find it a bit odd and confusing.

-- Michael Chermside







More information about the Python-list mailing list