difference with parenthese

chenyong20000 at gmail.com chenyong20000 at gmail.com
Mon Oct 17 04:52:27 EDT 2016


Hi,

i'm confused by a piece of code with parenthese as this:

----------------code 1------------------------------
>>> def outer():
...   def inner():
...     print 'inside inner'
...   return inner
...
>>> foo = outer()
>>> foo
<function inner at 0x9546668>
>>> foo()
inside inner



----------------code 2-------------------------------
>>> def outer():
...   def inner():
...     print 'inside inner'
...   return inner()
...
>>> foo = outer()
inside inner
>>> foo
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable


the difference between these two piece of code is that the code 2 has a "()" when "return inner".

My questions are:
(1) what is the function difference between these two pieces of code? what does "return inner()" and "return inner" mean?
(2) when foo = outer() is run, why output is defferent for two pieces of code?

thanks



More information about the Python-list mailing list