Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 559
gerrit Avatar asked Apr 08 '14 22:04

gerrit


People also ask

Why are parentheses optional when defining a class in Python?

A class definition is a bit different from a function/method definition. The parentheses in class definitions are for defining from which class you inherit. You don't write def in front of it, and when you inherit from 'object' which is the default you don't need the parentheses for the definition.

Why do some methods not have parentheses?

Attributes (e.g. imag) are like variables inside the object so you don't use parentheses to access them. Methods (e.g. islower()) are like functions inside the object so they do require parentheses to accept zero or more parameters and perform some work.

What does parentheses do in JavaScript?

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions. The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries.

Can a function be defined in a class?

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.


1 Answers

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

like image 174
Farmer Joe Avatar answered Sep 25 '22 20:09

Farmer Joe