confused about self, why not a reserved word?

Jean-Paul Calderone exarkun at divmod.com
Mon May 5 08:18:35 EDT 2008


On Mon, 5 May 2008 04:57:25 -0700 (PDT), globalrev <skanemupp at yahoo.se> wrote:
>class Foo(object):
>	def Hello(self):
>		print "hi"
>
>object is purple, ie some sort of reserved word.

What your text editor of choice does with syntax coloring is its own business.
It doesn't really have anything to do with the language, except inasmuch as one
might hope that it is *somehow* based on it.

>why is self in black(ie a normal word) when it has special powers.
>replacing it with sel for example will cause an error when calling
>Hello.

Not true.

    exarkun at charm:~$ python -c '
    > class Foo(object):
    >     def Hello(monkeys):
    >         print "hi"
    >
    > Foo().Hello()
    > '
    hi
    exarkun at charm:~$

`self´ as the name of the first parameter to an instance method is
convention; beyond that, it has no special meaning in the language.

`object´, on the other hand, is a builtin name which refers to a
particular object.

Jean-Paul



More information about the Python-list mailing list