[Python-ideas] Make parenthesis optional in parameterless functions definitions

Steven D'Aprano steve at pearwood.info
Thu Mar 31 20:43:29 EDT 2016


On Fri, Apr 01, 2016 at 05:12:56AM +0500, Mahan Marwat wrote:

> > I don't think that the benefit (two fewer characters to type) is worth
> > the effort of learning the special case. Right now, the rule is simple:
> > the def keyword ALWAYS needs parentheses after the name of the function,
> > regardless of whether there is one argumemt, two arguments, twenty
> > arguments, or zero arguments. Why treat zero as special?
> 
> For readability (Readability counts).

What makes you think that removing the parens increases readability? I 
think it *decreases* readability, because now there is a inconstency 
between the zero argument case and every single other case. Now the 
reader looks at function definitions and every single function 
definition has the same pattern:

def name ( parameter-list ) :

(where the parameter-list might have one parameter, or ten, or zero). 
With your proposal the reader will *nearly always* see the consistent 
pattern:

def name ( parameter-list ) :

but very occasionally, maybe one time in a hundred functions, or a 
thousand, see:

def name :

and be surprised. Even if it is just for a millisecond, that doesn't 
help readability, it hurts it.


> > The less awkward characters we have, the more readable our code will be
> > > (Beautiful is better then ugly).
> > I don't think that function declarations missing the parens are more
> > beautiful than them with the parens.
> 
> Some people argued that function definition with parenthesis seems to them
> natural. But actually it seems to us natural, because we have been used to
> it a-lot. IMHO parenthesisless functions definitions are natural and
> readable.

Yes yes, you said those exact same words before. I disagree the first 
time you said it, and I still disagree now.


> The Zen also says:
> > Special cases aren't special enough to break the rules.
> > Why do you think that zero-argument functions are special enough to
> > justify breaking the rules? Just to save two characters?
> 
> I think empty parenthesis for no apparent reason does not make any sense.

Of course it makes sense. The parameter list is empty. How do you get an 
empty list? With list delimiters with nothing inside them. Are you 
confused by "thelist = []"?

Function parameter lists use round brackets instead of square brackets, 
but otherwise are the same. An empty parameter list is indicated by 
empty parameter list delimiters.


> > When I was a beginner, I found that it was very helpful that I 
> > *always* needed to use parens after functions:
> 
> We are beginner only for once and expert for life.

If I claim to be an expert, but am confused by an empty parameter list, 
then I shouldn't be surprised if others wonder how expert I actually am.

This matter boils down to a question of taste. You apparently don't like 
the look of "def spam()", I do. I think my experience supports the 
current requirement, you think that it hurts readability, I don't. 
Unless you can give some objective evidence that it hurts readability, 
you aren't going to convince me.


> Between you can asked in your novice level, that why there are empty
> parenthesis if a function don't have parameters?

Because the list of parameters is empty.


-- 
Steve


More information about the Python-ideas mailing list