Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between assigning a function with and without parentheses?

Tags:

python

What's the difference in python between

value = getValue()

and

value = getValue

?

like image 538
bzrr Avatar asked Nov 24 '25 08:11

bzrr


2 Answers

Using parenthesis calls the function where as not using them creates a reference to that function.

See below:

>>> def t():
...     return "Hi"
...
>>> a = t
>>> a
<function t at 0x01BECA70>
>>> a = t()
>>> a
'Hi'
>>>

Here is a good link to explain further: http://docs.python.org/2/tutorial/controlflow.html (scroll down to the "defining functions" part).

value = getValue() is a function call and assignment of the return value. It means "call function getValue with no arguments and make value refer to whatever it returns".

value = getValue says "make value refer to the same function that getValue refers to".

like image 24
Amber Avatar answered Nov 25 '25 21:11

Amber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!