Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the parentheses for at the end of Python method names? [duplicate]

I'm a beginner to Python and programming in general. Right now, I'm having trouble understanding the function of empty parentheses at the end of method names, built-in or user-created. For example, if I write:

print "This string will now be uppercase".upper()

...why is there an empty pair of parentheses after "upper?" Does it do anything? Is there a situation in which one would put something in there? Thanks!

like image 559
David Kennell Avatar asked Aug 20 '15 21:08

David Kennell


People also ask

What are the parentheses after a function Python?

The parentheses tell Python to execute the named function rather than just refer to the function. Python goes back and looks up the definition, and only then, executes the code inside the function definition. The term for this action is a function call or function invocation.

What does double parentheses mean in Python?

The use of a double parentheses is actually an indicator of one of Python's coolest features - and that is that functions are themselves, objects!

Why does Python use parentheses?

Broadly speaking, the primary use of parentheses in Python is to call an object. That is the reason why standard parentheses are sometimes called the "call operator." Aside from their main use, parentheses are also used to define generator expressions.

What goes inside the parentheses in a Python function?

Arguments, parameters, or input variables go within the brackets. These can be thought of as pieces of data you pass to a function for use within that function.


3 Answers

Because without those you are only referencing the method object. With them you tell Python you wanted to call the method.

In Python, functions and methods are first-order objects. You can store the method for later use without calling it, for example:

>>> "This string will now be uppercase".upper
<built-in method upper of str object at 0x1046c4270>
>>> get_uppercase = "This string will now be uppercase".upper
>>> get_uppercase()
'THIS STRING WILL NOW BE UPPERCASE'

Here get_uppercase stores a reference to the bound str.upper method of your string. Only when we then add () after the reference is the method actually called.

That the method takes no arguments here makes no difference. You still need to tell Python to do the actual call.

The (...) part then, is called a Call expression, listed explicitly as a separate type of expression in the Python documentation:

A call calls a callable object (e.g., a function) with a possibly empty series of arguments.

like image 178
Martijn Pieters Avatar answered Oct 23 '22 09:10

Martijn Pieters


the parentheses indicate that you want to call the method

upper() returns the value of the method applied to the string

if you simply say upper, then it returns a method, not the value you get when the method is applied

>>> print "This string will now be uppercase".upper
<built-in method upper of str object at 0x7ff41585fe40>
>>> 
like image 27
gefei Avatar answered Oct 23 '22 10:10

gefei


upper() is a command asking the upper method to run, while upper is a reference to the method itself. For example,

upper2 = 'Michael'.upper
upper2() # does the same thing as 'Michael'.upper() !
like image 23
Michael Currie Avatar answered Oct 23 '22 11:10

Michael Currie