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

Chris Angelico rosuav at gmail.com
Thu Mar 31 23:00:26 EDT 2016


On Fri, Apr 1, 2016 at 1:37 PM, Joao S. O. Bueno <jsbueno at python.org.br> wrote:
> On 31 March 2016 at 14:57, Steven D'Aprano <steve at pearwood.info> wrote:
>> 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?
>
> Because class definitions already do so?

Class definitions aren't quite the same thing. A function *must* have
a set of arguments, and a function call *must* have a set of
parameters. But you can think about a class definition without ever
thinking about inheritance; the fact that all objects are instances of
'object' doesn't mean you have to say so every time you construct a
class. When I explain classes and the "is-a" relationship to my
students, it's easier to pretend that classes completely stand alone
than to dig into the fact that, in Python's world, your Dog (subclass
of Animal) is actually closely related to ValueError (subclass of
Exception).

If Python (or a style guide) mandated that classes MUST declare what
they're subclassing, it would want to say "(object)", not "()". No
class in Python 3 can truly have an empty inheritance list.

ChrisA


More information about the Python-ideas mailing list