Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100878
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: A newbie quesiton: local variable in a nested funciton |
| Date | 2015-12-26 14:44 +1100 |
| Message-ID | <mailman.17.1451101449.11925.python-list@python.org> (permalink) |
| References | <d070aa0d-e80f-4efb-a424-351737ddb2fc@googlegroups.com> |
On Sat, Dec 26, 2015 at 2:06 PM, <jfong@ms4.hinet.net> 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 inspired by it, variable scoping is defined by
declarations that say "here begins a variable".
> Here is the test codes in file test1.py:
> --------
> def outerf():
> counter = 55
> def innerf():
> print(counter)
> #counter += 1
> return innerf
>
> myf = outerf()
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()
{
write("%d\n", counter);
int counter;
counter += 1;
}
return innerf;
}
Based on that, I think you can see that having a variable declaration
in the function turns things into nonsense. What you're actually
wanting here is to NOT have the "int counter;" line, such that the
name 'counter' refers to the outerf one.
In Python, assignment inside a function creates a local variable,
unless you declare otherwise. To make your example work, all you need
is one statement:
nonlocal counter
That'll cause the name 'counter' inside innerf to refer to the same
thing as it does in outerf.
Hope that helps!
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-25 19:06 -0800
Re: A newbie quesiton: local variable in a nested funciton Ben Finney <ben+python@benfinney.id.au> - 2015-12-26 14:41 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 00:56 -0800
Re: A newbie quesiton: local variable in a nested funciton Ben Finney <ben+python@benfinney.id.au> - 2015-12-26 20:37 +1100
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-26 14:44 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 01:07 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-26 20:49 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 20:05 -0800
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 20:11 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-27 17:32 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-27 17:02 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-27 17:22 +1100
csiph-web