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

Steven D'Aprano steve at pearwood.info
Thu Mar 31 13:57:41 EDT 2016


On Thu, Mar 31, 2016 at 09:29:36PM +0500, Mahan Marwat wrote:

> I have an idea of making parenthesis optional for functions having no
> parameters. i.e
> 
> def greet: # note the missing parenthesis
>     print('hello')

-1

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?


> 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.


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?


> 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.

When I was a beginner, I found that it was very helpful that I *always* 
needed to use parens after functions:

def func(): ...

# later

result = func()


Otherwise I would forget the parens, and get a syntax error:

def func: ... 

or a strange and mysterious (well, mysterious to me, as a beginner) 
error:

result = func  # oops, forgot to call the func
answer = result + 1  # TypeError


So I think that is a positive feature that we have to write parens after 
function declarations, even for zero-argument functions. I think it 
would make Python a lesser language to make that optional:

- one more special case to memorise;
- loss of a good, helpful reminder for beginners that parens are needed 
  for both function definitions and function calls.

And for what gain? Saving a couple of characters. It's not worth it.


> In Python we have already adopted this. i.e parenthesis are optional in
> `if` statements, in class definition, in tuple etc

I think you are confused about these. Except for the class definition, 
where it is optional for backwards compatibility with Python 2 "classic 
classes", in all the other cases it isn't so much that parens are 
optional as that parens are not part of the syntax. You can *add* 
parens nearly everywhere, where they just act to group expressions:

> if x == 1: # parenthesis are optional here
>     pass

if (x) == (1):  # parens are just used for grouping expressions
if (x == 1):  # still just used for grouping expressions

In both cases, it's a waste of time to group the expressions, but it's 
not an error to do so.


> class Greet: # now we don't explicitly inherit from `object`
>     pass

This is a good example. In my opinion, I'm not actually happy that 
classes will *implicitly* inherit from object. But it is necessary for 
backwards compatibility.


> Tuple = 1, 2, 3 # parenthesis are optional here too

Again, the parens are not part of the syntax for tuples, except for the 
zero-length tuple (), so they're only used for grouping.



-- 
Steven


More information about the Python-ideas mailing list