[Python-ideas] Sorry for yet another self discussion
Terry Reedy
tjreedy at udel.edu
Mon May 14 14:29:24 EDT 2018
On 5/10/2018 3:58 PM, stefano wrote:
> I know that "self" parameter have been discussed a lot, but still I didn't
> find this proposal. If it was instead take my sincere apologies and please
> forget this mail.
>
> The disturbing part of the "self parameter" is the asymmetry of the
> definition and the call.
You are free to use the full signature of the method by calling the
method on the class, where it is defined. Self *is* a parameter of the
function and therefore *is* part of its signature.
>>> l = []
>>> l.append(1)
>>> list.append(l, 2)
>>> l
[1, 2]
Note that you can make a function defined outside of any class a method
of some class by making it an attribute of the class.
An intelligent editor like IDLE changes the call signature tip according
to the call form. "l.append(" brings up "(object, /)" while
"list.append(" brings up "(self, object, /)", along with a note
explaining that '/' marks the preceding arguments as positional-only.
> So I was thinking: why not do define the methods
> like: "def self.whatevermethod(par1, par2, etc)" instead of "def
> whatevermethod(self, par1, par2, etc)"?
This or something like it has been proposed and rejected before.
> This will allow the call and the definition to be written exactly in the
> same way, still leaving the "clarity" of the additional input for the
> function.
When you call 'self.method', you are not calling type(self).method. You
are instead calling the bound method 'self.method', which has the
abbreviated signature. This proposal would create an asymmetry between
the function definition and the call of the function.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list