Re: A newbie quesiton: local variable in a nested funciton

2015-12-27 Thread jfong
Chris Angelico at 2015/12/27 UTC+8 2:32:32PM wrote: > On Sun, Dec 27, 2015 at 3:11 PM, wrote: > > Last night I noticed that Python does not resolve name in "def" during > > import, as C does in the compile/link stage, it was deferred until it was > > referenced (i.e.

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread jfong
Chris Angelico at 2015/12/26 UTC+8 11:44:21AM wrote: > Pike is semantically very similar to Python, but it uses C-like > variable scoping. Here's an equivalent, which might help with > comprehension: > > function outerf() > { > int counter = 55; > void innerf() > { >

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread Ben Finney
jf...@ms4.hinet.net writes: > Thank you, Ben. It's amazing that you seem to know every piece of > Python information hiding in the web:-) You're welcome, I'm glad to help. As for the “hiding”, the answer is in the Python documentation itself. > see this question listed in python core language's

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread jfong
Ben Finney at 2015/12/26 UTC+8 11:42:08AM wrote: > The Python FAQ answers this, even using an example the same as yours > . > Thank you, Ben. It's amazing that you seem to know

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread Chris Angelico
On Sat, Dec 26, 2015 at 8:07 PM, wrote: > Thank you for the explanation. It reminds me to dig out something which seems > I had been read before. It's about nested scope in the book "Learning Python" > by Mark Lutz. > > "An assignment (X = value) creates or changes the

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread jfong
Chris Angelico at 2015/12/26 UTC+8 5:50:07PM wrote: > 11: Another normal assignment, because otherwise the rest of the work > is pointless. :) Thanks for this detailed example. As I had learned so far, Python really take "name" seriously, and every meaningful result you got have to assign to a

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread Chris Angelico
On Sun, Dec 27, 2015 at 3:11 PM, wrote: > Last night I noticed that Python does not resolve name in "def" during > import, as C does in the compile/link stage, it was deferred until it was > referenced (i.e. codes was executed). That's OK for Anyway codes has to be >

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread jfong
Last night I noticed that Python does not resolve name in "def" during import, as C does in the compile/link stage, it was deferred until it was referenced (i.e. codes was executed). That's OK for Anyway codes has to be debugged sooner or later. I just have to get used to this style. But check

Re: A newbie quesiton: local variable in a nested funciton

2015-12-26 Thread Chris Angelico
On Sun, Dec 27, 2015 at 3:05 PM, wrote: >> Python's flexibility and simplicity are a huge part of why I love the >> language so much. > > simplicity? Maybe because you are s familiar with Python. It's not to me, > at least at this moment. Please see my next question

A newbie quesiton: local variable in a nested funciton

2015-12-25 Thread jfong
As a tranditional language programmer like me, the result is really weird. Here is the test codes in file test1.py: def outerf(): counter = 55 def innerf(): print(counter) #counter += 1 return innerf myf = outerf() the result is: >>> import

Re: A newbie quesiton: local variable in a nested funciton

2015-12-25 Thread Ben Finney
jf...@ms4.hinet.net writes: > In the first situation, the local variable 'counter' can be referenced > correctly. But in the second, why a statement added after the print() > statement can makes this variable "disappear", even the print() won't > do the right thing. Isn't it wired? please help!

Re: A newbie quesiton: local variable in a nested funciton

2015-12-25 Thread Chris Angelico
On Sat, Dec 26, 2015 at 2:06 PM, wrote: > As a tranditional language programmer like me, the result is really weird. By "traditional", I'm guessing you mean that you know C-like languages (Java, ECMAScript/JavaScript, etc). In C, and in many languages derived from or