difference with parenthese

2016-10-17 Thread chenyong20000
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
-- 
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 chenyong20000
Hi Wolfgang,

thanks for your kind reply. I got.


regards
skyworld

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


function call questions

2016-10-18 Thread chenyong20000
Hi,
I got a function call as this:

>>>def add_to_tree(root,value_string):
...print "root is %s, value_string is %s" % (root, value_string)
...for ch in value_string:
...  print "ch is %s" % ch
...  root = root.setdefault(ch,{})
...  print "root is", root
...  print "tree is", tree
...
>>> tree={}
>>> txt='abc'
>>> add_to_tree(tree,txt)
root is {}, value_string is abc
ch is a
root is {}
tree is {'a': {}}
ch is b
root is {}
tree is {'a': {'b': {}}}
ch is c
root is {}
tree is {'a': {'b': {'c': {

My question is:
(1) why root is always {}?
(2) why tree is {'a': {'b': {'c': {?
(3) why root isn't the same as tree? shouldn't they be the same because tree is 
argument passed as root?


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


Re: function call questions

2016-10-18 Thread chenyong20000
> > My question is:
> > (1) why root is always {}?
> 
> Because that's what you bind it to in the line
> 
> > ...  root = root.setdefault(ch,{})
> 
> See 
> for a description of the the setdefault() method.

I'm still confused here. root.setdefault(ch,{}) should return {'a':{}}, right? 
then this dictionary is referenced by root by "root = root.setdefault(ch,{})". 
why root is an empty dictionary now?


> 
> > (2) why tree is {'a': {'b': {'c': {?
> 
> Basically the same answer -- you bind root to the innermost dict and that's 
> where you insert the ch key on the next iteration of the for loop
> .
> > (3) why root isn't the same as tree? shouldn't they be the same because
> > tree is argument passed as root?
> 
> You should know the answer by now ;)

In fact i'm still confused by these answers. Could you please described more? 
thanks

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


Re: function call questions

2016-10-18 Thread chenyong20000
Thanks Peter and Anssi for your kind help. Now I'm ok with the first question. 
But the second question still confused me. Why "it seems that 
after root = root.setdefault(ch,{}) tree['a'] and root are the same 
object" and follows tree['a']['b']? Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-18 Thread chenyong20000
在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
> On 2016-10-19 03:15, [email protected] wrote:
> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first 
> > question. But the second question still confused me. Why "it seems that
> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
> > object" and follows tree['a']['b']? Thanks.
> >
> You call the .setdefault method with a key and a default value.
> 
> The .setdefault method does this: if the key is in the dict, it returns 
> the associated value, else it puts the key and the default into the dict 
> and then returns the default.

thanks for the reply. I understand this. I'm now confused on why tree got its 
magic result.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-19 Thread chenyong20000
在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道:
> [email protected] wrote:
> 
> > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
> >> On 2016-10-19 03:15, [email protected] wrote:
> >> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first
> >> > question. But the second question still confused me. Why "it seems that
> >> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
> >> > object" and follows tree['a']['b']? Thanks.
> >> >
> >> You call the .setdefault method with a key and a default value.
> >> 
> >> The .setdefault method does this: if the key is in the dict, it returns
> >> the associated value, else it puts the key and the default into the dict
> >> and then returns the default.
> > 
> > thanks for the reply. I understand this. I'm now confused on why tree got
> > its magic result.
> 
> Perhaps it is easier to understand if you rewrite the function without 
> setdefault()?
> 
> def add_to_tree(root, value_string):
> print "root is %s, value_string is %s" % (root, value_string)
> for ch in value_string:
> print "ch is %s" % ch
> if ch not in root: # always true if the root arg is an empty dict
> root[ch] = {}
> root = root[ch] # inside the function the inner dict becomes the new
> # root
> print "root is", root
> print "tree is", tree

please forgive my stupid. I still can't follow this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-20 Thread chenyong20000
在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道:
> wrote in message 
> news:[email protected]...
> 
> 在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道:
> > [email protected] wrote:
> >
> > > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
> > >> On 2016-10-19 03:15, [email protected] wrote:
> > >> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first
> > >> > question. But the second question still confused me. Why "it seems 
> > >> > that
> > >> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
> > >> > object" and follows tree['a']['b']? Thanks.
> > >> >
> 
> > please forgive my stupid. I still can't follow this.
> 
> Let's see if I can explain. I am using 't' and 'r' instead of 'tree' and 
> 'root', but otherwise it is the same as your original example.
> 
> >>> t = {}
> >>> r = t
> >>> id(t)
> 2542235910088
> >>> id(r)
> 2542235910088
> 
> At this point, t and r are both references to the same empty dictionary.
> 
> >>> r = r.setdefault('a', {})
> 
> This has done two things.
> 
> It has inserted the key 'a' into the dictionary, and set its value to {}.
> 
> >>> t
> {'a': {}}
> >>> id(t)
> 2542235910088
> 
> It has also rebound 'r' so that it now references the new empty dictionary 
> that has been inserted.
> 
> >>> r
> {}
> >>> id(r)
> 2542234429896
> >>>t['a']
> {}
> >>> id(t['a'])
> 2542234429896
> 
> Now continue this process with r = r.setdefault('b', {}), and watch what 
> happens.
> 
> Hopefully this will help you to understand. Feel free to ask further if not 
> sure.
> 
> Frank Millman

Hi Frank,

thanks very much for your kind help. Your reply is clear. But I'm hindered by 
those you've not explained.

I'm always confused by "r = r.setdefault('a', {})". when the first loop 
finished, as what you have pointed out,
> >>> t
> {'a': {}}
> >>> r
> {}

Then next "r = r.setdefault('b', {})" will run again. Here what is "r" in 
"r.setdefault('b',{})"? According to final result, it should be "t['a']", which 
I can't understand. I thought the command is r.setdefault, so it should still 
be last "r", i.e., empty {}. Could you please let me know what I missed? 
thanks. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-22 Thread chenyong20000
在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道:
> wrote in message 
> news:[email protected]...
> 
> 在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道:
> > wrote in message
> > news:[email protected]...
> >
> > > Let's see if I can explain. I am using 't' and 'r' instead of 'tree' and
> > > 'root', but otherwise it is the same as your original example.
> > >
> > > >>> t = {}
> > > >>> r = t
> > > >>> id(t)
> > > 2542235910088
> > > >>> id(r)
> > > 2542235910088
> > >
> > > At this point, t and r are both references to the same empty dictionary.
> > >
> > > >>> r = r.setdefault('a', {})
> > >
> > > This has done two things.
> > >
> > > It has inserted the key 'a' into the dictionary, and set its value to 
> > > {}.
> > >
> > > >>> t
> > > {'a': {}}
> > > >>> id(t)
> > > 2542235910088
> > >
> > > It has also rebound 'r' so that it now references the new empty 
> > > dictionary
> > > that has been inserted.
> > >
> > > >>> r
> > > {}
> > > >>> id(r)
> > > 2542234429896
> > > >>>t['a']
> > > {}
> > > >>> id(t['a'])
> > > 2542234429896
> > >
> > > Now continue this process with r = r.setdefault('b', {}), and watch what 
> > > happens.
> >
> > thanks very much for your kind help. Your reply is clear. But I'm hindered 
> > by those you've not explained.
> >
> > I'm always confused by "r = r.setdefault('a', {})". when the first loop 
> > finished, as what you have pointed out,
> > > >>> t
> > > {'a': {}}
> > > >>> r
> > > {}
> >
> > Then next "r = r.setdefault('b', {})" will run again. Here what is "r" in 
> > "r.setdefault('b',{})"? According to final result, it should be "t['a']", 
> > which I can't understand. I thought the command is r.setdefault, so it 
> > should still be last "r", i.e., empty {}. Could you please let me know 
> > what I missed? thanks.
> 
> Firstly, I want to explain more clearly what I am doing here.
> 
> Instead of running your loop 3 times, I am running your command three times 
> one step at a time (though I only showed the first one).
> 
> >>> t = {}
> >>> r = t
> >>> r = r.setdefault('a', {})
> >>> r = r.setdefault('b', {})
> >>> r = r.setdefault('c', {})
> 
> This should give exactly the same result as your loop. The benefit of 
> running it this way is that you can check the values after each step.
> 
> May I suggest that you do this, and try to understand the contents of 't' 
> and 'r' at each point. If you are still unsure, let us know at which point 
> the values are not what you expect, and I will try to explain further.
> 
> It is important that you understand that you are rebinding 'r' at each step, 
> so after each command, 'r' is no  longer referencing the same object that it 
> was referencing in the previous step.
> 
> To see the difference, try running it it this way -
> 
> >>> t = {}
> >>> r = t
> >>> r.setdefault('a', {})
> >>> r.setdefault('b', {})
> >>> r.setdefault('c', {})
> 
> Hope this helps.
> 
> Frank
Hi Frank,

thanks for your kind help. What confused me is at this line:

> >>> r = r.setdefault('b', {})

and its previous one

> >>> r = r.setdefault('a', {})

When r.setdefault('a',{}) is run, I understand it will return an empty {}. At 
this time both r & t reference to {'a':{}}, right? So when "r = 
r.setdefault('a',{})" is run, r reference to {} while t keeps the same as 
{'a':{}}.

then comes r.setdefault('b',{}). What hinder me is here. since r has changed 
its reference to {}, r.setdefault('b',{}) will return {} again. So what does 
this done to t? why at this time t changes to {'a':'b':{}}? Sorry for my silly 
here. Thanks


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


Re: function call questions

2016-10-22 Thread chenyong20000
在 2016年10月22日星期六 UTC+8下午5:06:22,Frank Millman写道:
> wrote in message 
> news:[email protected]...
> 
> 在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道:
> > wrote in message
> > news:[email protected]...
> >
> > Hi Frank,
> >
> > thanks for your kind help. What confused me is at this line:
> >
> > >>> r = r.setdefault('b', {})
> >
> > and its previous one
> >
> > >>> r = r.setdefault('a', {})
> >
> > When r.setdefault('a',{}) is run, I understand it will return an empty {}. 
> > At this time both r & t reference to {'a':{}}, right? So when "r = 
> > r.setdefault('a',{})" is run, r reference to {} while t keeps the same as 
> > {'a':{}}.
> >
> > then comes r.setdefault('b',{}). What hinder me is here. since r has 
> > changed its reference to {}, r.setdefault('b',{}) will return {} again. So 
> > what does this done to t? why at this time t changes to {'a':'b':{}}? 
> > Sorry for my silly here. Thanks
> >
> 
> I don't know if you have been following the other posts in this thread.
> 
> There were some posts from Anssi Saari who was also confused by this, but 
> then yesterday he sent a post saying that he has now 'got it'.
> 
> I don't think I can do better than quote his explanation of what is 
> happening -
> 
> """
> OK, so what happens is that now t references the dictionary with {'a': {}} 
> and r references the empty dict inside that.
> 
> So when we assign to r again, it's the empty dict inside t (the one accessed 
> by key 'a') that changes to {'b': {}} and t becomes {'a': {'b': {}}}.
> """
> 
> That is exactly right.
> 
> Does that help?
> 
> Frank

Hi Frank,

I have read Anssi's post already before I sent the post. To be frankly, I can't 
understand why he got the right answer. I'm sorry for my silly. "So when we 
assign to r again, it's the empty dict inside t (the one accessed 
by key 'a')". I do can't understand why this happens. that is the reason why I 
have asked for this once again and again. There must be some import point I 
missed but I don't what is it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-23 Thread chenyong20000
在 2016年10月22日星期六 UTC+8下午9:15:06,Frank Millman写道:
> wrote in message
> news:[email protected]...
> 
> > I have read Anssi's post already before I sent the post. To be frankly, I
> can't understand why he got the right answer. I'm sorry for my silly. "So
> when we assign to r again, it's the empty dict inside t (the one accessed
> by key 'a')". I do can't understand why this happens. that is the reason why
> I have asked for this once again and again. There must be some import point
> I missed but I don't what is it.
> 
> Let's try this -
> 
> >>> t = {}
> >>> r = t
> >>> r = r.setdefault('a', {})
> >>> t
> {'a': {}}
> 
> I think you are happy up to this point.
> 
> We now have three objects -
> 
> "t" is a dictionary
> 'a' is a key in the dictionary
> {} is the value associated with the key 'a' in "t"
> 
> I think you are happy up to this point.
> 
> The question is, what is "r"?
> 
> Before the assignment, "r" was a reference to the dictionary referenced by 
> "t".
> 
> After the assignment, "r" no longer refers to "t". It is now a reference to 
> the
> third object listed above, the {} that is the value associated with the key 
> 'a'.
> 
> >>> t
> {'a': {}}
> >>> t['a']
> {}
> >>> r
> {}
> >>> t['a] is r
> True
> 
> Keep looking at this until it sinks in. "r" and "t['a']" are *the same 
> object*. We just have two ways of accessing it.
> 
> Try adding some key/values to the empty dictionary -
> 
> >>> r['x'] = 99
> >>> r
> {'x': 99}
> >>> t['a']
> {'x': 99}
> >>> t
> {'a': {'x': 99}}
> 
> I will pause at this point, and give you a moment to absorb that.
> 
> Hopefully, the penny will drop and everything will become clear.
> 
> If not, let us know which of the above steps you do not understand.
> 
> Good luck - keep plugging away, and you will get there :-)
> 
> Frank
> 
> P.S. I assume you understand that the lines prefixed with '>>>' are to be 
> entered while in the python interpreter. It is really important that you 
> type these lines in yourself and examine the results.

Hi Frank,

I got it this time. Thanks very much for your help. I thought r is an empty 
dictionary without any connection to t before. Now I know that happened. Thanks.


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