Do I need "self" and "other"?
Robert Kern
robert.kern at gmail.com
Fri Jun 27 20:10:56 EDT 2008
Terry Reedy wrote:
>
> Kurda Yon wrote:
>>
>> OK, I see. In the given example "self" is just a name which can be
>> replace by whichever (valid) name. Is that always like that? I mean,
>> does "slef" have a special meaning in some cases or it is always "just
>> a name like any other"?
>
> Yes.
>
> A method is a function bound to a class or instance thereof.
> Def statements create functions. Parameter names are arbitrary, as long
> as they do not conflict with any global names you want to access from
> within the function.
>
> Self (and other) are simply community conventions. They do have they
> advantage that if they are only used as function/method parameter names,
> then they will not conflict with any module globals.
It's worth noting that 'self' for the first parameter of a method is an
extremely strong convention. I highly encourage you to follow it. In particular,
classmethods and staticmethods don't take an instance of the class as the first
argument, so using 'self' for instance methods, 'cls' for classmethods, and
nothing in particular for staticmethods (since the first argument isn't special
at all), helps distinguish them when reading. You risk annoying your reader by
using something other than 'self' in an instance method.
By contrast, using 'other' for the other argument to a binary __mathoperation__
method is not a particularly strong convention. No one will be annoyed if you
use something else.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list