When to and Why Use Parentheses in Python

One of the important (and usually confusing) concepts a developer needs to understand in Python is the use (or omission) of parentheses. This may seem so trivial, even obvious but it’s something that can cause problems to a lot of programmers, especially new ones coming from a different programming language.

At best it could cause your program to raise an exception and at worse your codes do something you never intended it to do.

But why is this important for Python programmers and not for every other programming languages?

Because in Python (and some other languages) ….

Functions are treated as first-class citizens

You can research what first-class citizens (or first-class-functions) are, but it simply means you can pass functions as parameters, return them as value, assign them to a variable or include in a structure.

If you’re using a programming language that does not treat functions as first-class citizens, then you won’t have this issue since you won’t even be able to run or compile your code at all.

Let’s write a code to better understand this concept.

For this, we’ll create a class called “Human” with 2 properties and 1 method:

Next, we’ll run our code interactively (you can do this by adding “-i” before the filename) and once we’re inside the interactive shell, we’ll create an instance of the Human class:

The general rule in distinguishing between a property and a function (or method) is that a property doesn’t end in parentheses, but a function/method does.

So let’s now access the 2 properties of the person object:

Notice that the ‘name’ and ‘birthdate’ were accessed without parentheses.

Next, we invoke the ‘age’ method:

Notice how we added parentheses to it?

So, what’s the problem?

The problem arises when we don’t know the difference between a function access with and without parentheses.

For example, say we want to store the age of the person object to another variable and we typed this:

>>> person_age = person.age

Notice that there are no parentheses?

This could easily happen to any developer who either forgot that “age” is a method (and not a property) or does not know the difference.

Let’s inspect the “person_age” variable:

As you can see, it’s a method — not the number (81) we saw when we used parentheses.

You can still use this by just adding the parentheses when you want to access the value (ie execute the method):

Summary

Remember this, when you use a function or method by its name alone (without parentheses), you are merely getting a reference to that function/method and that you’re not executing it.

If you add parentheses to the function/method name, you are executing it.

>>> age = person.age        # storing the function to a variable

>>> age = person.age()      # executing the function, storing the return value to a variable

Of course, you could avoid confusion in the first place by naming accordingly like this:

age = person.get_age()        # 'get_age' won't be mistaken for a property but seen as a method

You may also use the @property decorator to convert the age method to a property like this:

That’s all you have to do, and when you go back and try it, the “age” now acts just like a property.

Notice how we no longer added parentheses to age?

Of course, there are times you actually and intentionally want to pass the function object instead of invoking it, and that’s what we’ll cover in the next blog so stay tuned!

Leave a Comment