[Python-ideas] Sorry for yet another self discussion

Steven D'Aprano steve at pearwood.info
Thu May 10 18:33:48 EDT 2018


On Thu, May 10, 2018 at 07:58:12PM +0000, stefano wrote:

> The disturbing part of the "self parameter" is the asymmetry of the
> definition and the call.

Why is that disturbing? There is always some asymmetry between 
*defining* a function and *calling* a function.

Function definitions:

    def function(named parameter list):
        block

Function calls:

    function(positional arguments)


We don't repeat the keyword "def" or the colon in the call, and we 
obviously don't repeat the body of the block. We typically call the 
function with either positional arguments, or only a *subset* of the 
keyword arguments, relying on defaults to fill in the rest. The 
arguments we provide will often be expressions, sometimes complex 
expressions, not just simple names. Even if they are simple names, they 
rarely match the parameter name:

Definition:
    function(spam, eggs, cheese=None, aardvark=True)

Call:
    function((foo + bar) or baz, foobar)

So whatever symmetry there is, it isn't much.


> 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)"?

"Why not ..." is the wrong question. Adding new features to the 
language, or new syntax, is not Default Allow ("add the feature, unless 
there is a compelling reason not to"). There has to be a good, 
positive reason for adding the feature, not merely a lack of reason 
not to.

What does this feature buy us? Not much.

It doesn't add any new expressiveness to the language. It doesn't let 
the language do anything that couldn't be done before.

What it does do is add complexity to the syntax, there now being two 
ways to specify the first parameter:

    def self.method(arg)
    def method(self, arg)

both of which need to be supported, which means more complexity to the 
language, more documentation needed, more for people to learn, more for 
tutorials to have to cover, more tests needed, etc, and all those costs 
don't actually gain us any significant advantage for the code we write.

And what happens with classmethods and staticmethods, or those people 
who for whatever reason refuse to use the name "self" for the first 
parameter?


-- 
Steve


More information about the Python-ideas mailing list