The need to put "self" in every method

Roy Smith roy at panix.com
Tue May 31 12:52:20 EDT 2005


Fernando M. <fernandomirandamuro at gmail.com> wrote:
>i was just wondering about the need to put "self" as the first
>parameter in every method a class has because, if it's always needed,
>why the obligation to write it? couldn't it be implicit?

Didn't this exact question get asked just a few days ago?

Anyway, the short answer is:

1) Yes, it's always needed.
2) It's part of the Python design philosophy that "explicit is better
than implicit".

Note that in languages like C++ where using "this->" is optional,
people invent their own conventions for keeping local and instance
variables distinct, like prepending m_ to member names (except that
different people do it different ways, so it's more confusing).




More information about the Python-list mailing list