分配带括号和不带括号的功能有什么区别?

时间:2013-08-04 22:50:21

标签: python

之间的python有什么区别
value = getValue()

value = getValue

2 个答案:

答案 0 :(得分:11)

使用括号调用不使用它们的函数创建对该函数的引用。

见下文:

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

这是一个很好的链接,可以进一步解释:http://docs.python.org/2/tutorial/controlflow.html(向下滚动到“定义函数”部分)。

答案 1 :(得分:5)

value = getValue()是函数调用和返回值的赋值。它表示“没有参数的调用函数getValue,并使value引用return s的任何内容。”

value = getValue说“make value引用getValue引用的相同功能”。