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 Python Basics Functions and Looping Functions

170597306460198501940249800169706170459385019750203950
170597306460198501940249800169706170459385019750203950
1,552 Points

the "( )" after "text.upper( )"

English is not my first language so sometimes I keep scrubbing the video to understand what Craig is saying, and now I think I didn't get why a blank "( )" is needed after the "text.upper( )".

Why is this needed again? It also helps if I can be directed to a video where he discusses about it :)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The parens () signify that the method should be called. Without the parens, text.upper is a reference to the method.

In Python, methods and functions are โ€œfirst-class citizensโ€ and may be referenced directly, assigned to a variable, and passed to other functions when the parens are not used.

Post back if you need more help. Good luck!!!

the follow-up question I have is, I get that the functions get priority but why not still use parens simplicity ? or is there more to it?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

There are solid use cases for executing the function or method vs getting just a reference to the function or method.

The parens are used when arguments are passed to the function during its execution. If no arguments are needed, then a pair of empty parens are used to say โ€œexecute, but no arguments are provided.โ€

With no parens, no execution of the function occurs. Instead a reference to the function is used.

One example,

def change_text(text, func):
    return func(text)

print(change_text('abcde', str.upper))
print(change_text('abcde', str.capitalize))

Outputs:

ABCDE
Abcde

Another less obvious use of passing a function is decorators. A decorated function is passed as an unexecuted function and an updated unexecuted function is returned in its place.