Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Special Methods

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

When to use parentheses or not with attributes, methods, and functions.

Something I don't think I understand, fully.

Sometimes, Kenneth uses parentheses after his functions and methods. Sometimes, he doesn't.

I'm not sure what the rule is for this.

Why do I need to use parentheses and when should I not use parenthesis when creating a function/method?

2 Answers

Jeffrey Duarte
Jeffrey Duarte
9,706 Points

Hi Stephen,

Whenever you come across this concern you should ask yourself if you're looking to reference, update or execute the variable.

Attributes will never have parenthesis since they cannot be executed.

Classes have parenthesis because when initially called you're most likely looking to execute the creation of a new object of that class.

Methods will have parenthesis because once created they're meant to be called.

For example:

class Car:
    # Attributes
    color = 'red'
    seats = 4

    # Methods
    def drive():
        return 'Car moved forward 1 mile.'

# Below I create my_car from the Car class.
my_car = Car()

# Below I'm updating the color attribute for my_car.
my_car.color = 'white'

# Below I am referencing the color of my_car.
print('My car is currently {}.'.format(my_car.color))

"""The following would be an execution of a method and would return 'Car moved forward 1 mile.'
Without the parenthesis you would get an error as the method can't simply be referenced but must be called."""
my_car.drive()

I hope this helps.

Iulia Maria Lungu
Iulia Maria Lungu
16,935 Points

Careful, your last statement is incorrect, Jeffrey Duarte.

Without the parenthesis you would get an error as the method can't simply be referenced but must be called.

my_car.drive with no calling parenthesis would point to the Car class method definition. It won't throw an error. The output would be something like : <bound method Car.drive of <__main__.Car object at 0x109b61f60>>

daniel sousa
daniel sousa
7,551 Points

I think the reason that Stephen is confused here is the same reason I was confused - You don't need to call a function if its a property apparently?