Why the 'self' argument?

Terry Reedy tjreedy at udel.edu
Sat Sep 6 16:17:03 EDT 2003


"Jonathan Aquino" <Jon_Aquino at shaw.ca> wrote in message
news:fc08a59c.0309060830.623d31c7 at posting.google.com...
> Two reasons I cringe when I see self as the first parameter of
method
> declarations (i.e. "def methodname(self, ...)" (I don't mind self in
> the method body)):
...
> 2) The number of parameters in the method call is one less than the
> number of parameters in the method declaration.   I'm annoyed when I
see
> this because I want the rules of the language to be obvious (in this
> case, a one-to-one mapping). I would be embarassed to have to
explain
> this to a beginning programmer.

Properly understood, consistency is what we have now and what the
proposal would break.  As others have said and as I further explained
in another response just posted, the full method function call is
klass.meth(inst, *rest) with a 1 to 1 matching of args and params like
all other function calls.  Deleting 'self' as an explicit param would
introduce an inconsistency that would have to be applied to all
function defs (see other post if not clear why).

The main virtue of the inst.meth(*rest) abbreviation, with klass made
implicit, is not just to save typing the klass name but to push
determination of which 'klass' to the runtime inheritance tree,
thereby saving much more programmer work.  (When using classes from a
library, the programmer may not even know which superclass has the
method.)  It also automatically propagate inheritance tree changes to
all method calls, which also saves programmer work.

Terry J. Reedy






More information about the Python-list mailing list