Why are parenthesis optional when defining a class, but mandatory when defining a function?

12,348

I think the answer to your question is simply syntax. That is just the way Python is set up, but my take on how it got that way is:

I would think functions came out of mathematics things like:

f(x) = x

So when computer programming languages were being created there seems to have been some logical continuity from analog mathematics into programming languages.

Classes on the other hand are more constructs of Computer Science, and repetitive memory management, so they were not created in such a fashion, but because they have a functional quality to them, they were given similar notation.

For Python, I will use the term method for function as that is the usual lingo...

I understand your argument that both a class and method should be allowed to be defined using a short-cut in the no argument case:

  • for classes when there is no inheritence
  • for methods when there are no arguments

One reason I can think of is for consistency across usage and definition. Let's look at some examples:

definition:

def funcA():
    return 0

def funcB(arg):
    return arg

and you want to call that funciton:

>>> funcA()
>>> functB("an argument")

and

>>> f1 = funcA
>>> f2 = funcB
>>> f1()
>>> f2("another argument")

to pass references and call them.

The syntax of the paranthesis between method declaration is consistent with calling the methods.

You need to put those empty parenthesis otherwise the interpreter will give you a reference to the method, and not actually call it.

So one benefit is it makes your code very clear.

definition:

class classA:
    pass

class classB(object):
    pass

usage:

# create an instance
my_instance_of_A = classA()
my_instance_of_B = classB()

# pass a reference
my_ref_to_A = classA
my_ref_to_B = classB

# call by reference
new_A = my_ref_to_A()
new_B = my_ref_to_B()

Here there is no change in behavior with regards to whether the class inherits or not, its calling behavior is dictated by what its internal or inherited __init__ method is defined as.

I think the current set up of requiring the empty () makes the code more readable to the untrained eye.

If you really really really want to do what you ask, there is a workaround... you could always do this:

func = lambda: "im a function declared with no arguments, and I didn't use parenthesis =p"

which can be called:

>>> func
<function <lambda> at 0x6ffffef26e0>
>>> func()
"im a function declared with no arguments, and I didn't use parenthesis =p"

But the python holy book says No

Share:
12,348
gerrit
Author by

gerrit

I'm a researcher in satellite remote sensing at the Deutscher Wetterdienst in Offenbach, Hessen, Germany. I first came to Stack Exchange for practical reasons: Tex.SE has been of major help when I wrote my licentiate thesis. Since then, I have discovered the joy of many websites. As my network profile will show, I'm interested in travel, outdoor, scientific skepticism, and as my work is related to academia, also in academia and LaTeX. When using a personal pronoun to refer to me in English, I prefer the pronoun he/him/his. I will not feel offended if you use another pronoun of your choice. Profile photo by Dobromila, CC By-SA, via Wikimedia Commons

Updated on June 14, 2022

Comments

  • gerrit
    gerrit almost 2 years

    In Python, defining a function with an empty parameter list requires a set of empty parenthesis. However, defining a class with the default superclass does not require a set of empty parenthesis; rather, those are optional, and appear to be uncommon. Why is it so?

    See also: Python class definition syntax.