Why do class methods always need 'self' as the first parameter?

rantingrick rantingrick at gmail.com
Mon Sep 5 23:58:30 EDT 2011


On Aug 31, 9:35 am, "T. Goodchild" <tgoodch... at gmail.com> wrote:
> I’m new to Python, and I love it.  The philosophy of the language (and
> of the community as a whole) is beautiful to me.

Welcome aboard mate!

> But one of the things that bugs me

Oh here we go! :-)

>  is the requirement that all class
> methods have 'self' as their first parameter.  On a gut level, to me
> this seems to be at odds with Python’s dedication to simplicity.

It will will seem odd at first. I too hated typing all those "selfs"
all the time. But believe me my new friend in no time those selfs will
roll of your fingers with great ease. You'll forget how much you hate
them and find much more to complain about.

Like for instance: I really lament the missing redundancy of Explicit
Lexical Scoping in python. For me global variables should have to be
qualified.

> For example, consider Python’s indent-sensitive syntax.  
> [...]
> and the result was a significantly improved
> signal-to-noise ratio in the readability of Python code.

Yes, forced indention is my favorite aspect of Python!

> So why is 'self' necessary on class methods?

It could be that Guido has a exaggerated self importance and just
liked the sound of all those selfs whist reading source code. However
i believe the real reason is really readability! It takes a while to
understand this aspect because the natural human response is to be
lazy (for instance i could have used "used to" in the previous
sentence if i was slothful). We are all inherently lazy beings who
need structure to keep us from spiraling out of control into the abyss
of selfishness.

GvR: Computer Scientist and Behavioral psychologist.



More information about the Python-list mailing list