Re: difference with parenthese

2016-10-17 Thread chenyong20000
Hi Wolfgang,

thanks for your kind reply. I got.


regards
skyworld

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: difference with parenthese

2016-10-17 Thread Wolfgang Maier

On 17.10.2016 16:45, chenyong20...@gmail.com wrote:

Hi Wolfgang,

thanks for your kind reply. I try to explain what I got from your reply:

for code1, when running "foo = outer()", since outer() is callable, function outer() is running and it 
returns an object, which referring to function inner(). When "foo" is running, it indicates it is referring 
to object inner(). When "foo()" is running, object inner() is called, so it prints "inside inner".

for code2, when running "foo = outer()", since outer() is callable, function outer() is running and returns 
results from function inner(), which prints "inside inner". so "foo" now is a string, which 
contains "inside inner". since this string isn't a function, foo() will make an error.

Do you think my understanding is right? thanks.


regards
skyworld chen



Not quite. Your understanding of code 1 is correct.
In your example code 2, however:

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

calling outer returns the result of inner, as you are saying, but that 
result is *not* the string 'inside inner', but None (implicitly 
returned). As I explained before, the printed string is a side-effect of 
the function inner, but it is *not* what that function returns.


So:

foo = outer()

*prints* the string 'inside inner', but sets foo to refer to None. 
That's why the following TypeError mentions NoneType.


Best,
Wolfgang

--
https://mail.python.org/mailman/listinfo/python-list


Re: difference with parenthese

2016-10-17 Thread chenyong20000
Hi Wolfgang,

thanks for your kind reply. I try to explain what I got from your reply:

for code1, when running "foo = outer()", since outer() is callable, function 
outer() is running and it returns an object, which referring to function 
inner(). When "foo" is running, it indicates it is referring to object inner(). 
When "foo()" is running, object inner() is called, so it prints "inside inner".

for code2, when running "foo = outer()", since outer() is callable, function 
outer() is running and returns results from function inner(), which prints 
"inside inner". so "foo" now is a string, which contains "inside inner". since 
this string isn't a function, foo() will make an error.

Do you think my understanding is right? thanks.


regards
skyworld chen

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: difference with parenthese

2016-10-17 Thread Wolfgang Maier

On 17.10.2016 10:52, chenyong20...@gmail.com wrote:

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



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 "", line 1, in 
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



I suggest you look at a simpler example first:

def func():
print('inside func')

func()
a = func
b = func()

print(a)
print(b)

and see what that gives you.
There are two things you need to know here:

- functions are callable objects and the syntax to call them (i.e., to 
have them executed) is to append parentheses to their name; without 
parentheses you are only referring to the object, but you are *not* 
calling it


- functions without an explicit return still return something and that 
something is the NoneType object None. So the function above has the 
side-effect of printing inside func, but it also returns None and these 
are two totally different things


Once you have understood this you can try to go back and study your 
original more complicated example.


Best,
Wolfgang

--
https://mail.python.org/mailman/listinfo/python-list