Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100878

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: A newbie quesiton: local variable in a nested funciton
Date Sat, 26 Dec 2015 14:44:01 +1100
Lines 52
Message-ID <mailman.17.1451101449.11925.python-list@python.org> (permalink)
References <d070aa0d-e80f-4efb-a424-351737ddb2fc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 19cT2K70ir+vm/a+zlFj5gLwsYltqfbbNKMEcl65/akg==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'received:209.85.223': 0.03; 'inspired': 0.05; 'assignment': 0.07; 'variable,': 0.07; 'cc:addr:python-list': 0.09; 'declarations': 0.09; 'derived': 0.09; 'def': 0.13; '"here': 0.16; '"int': 0.16; '(java,': 0.16; '2:06': 0.16; 'declaration': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guessing': 0.16; 'helps!': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'subject:variable': 0.16; 'weird.': 0.16; 'wrote:': 0.16; 'refers': 0.18; 'creates': 0.18; 'programmer': 0.18; 'variable': 0.18; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; 'void': 0.22; 'defined': 0.23; 'dec': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'message- id:@mail.gmail.com': 0.27; 'otherwise.': 0.27; 'turns': 0.27; 'function': 0.28; 'equivalent,': 0.29; 'etc).': 0.29; "i'm": 0.30; '--------': 0.32; 'int': 0.33; 'similar': 0.33; 'languages': 0.34; 'file': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'things': 0.38; 'received:209': 0.38; 'mean': 0.38; 'test': 0.39; 'does': 0.39; 'skip:e 20': 0.39; 'your': 0.60; 'hope': 0.61; 'wanting': 0.66; 'here': 0.66; '26,': 0.72; 'chrisa': 0.84; 'nonsense.': 0.84; 'pike': 0.84; "that'll": 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=PdB61/MV+2mA8wR7wN0jVMAVz/+YUZrMCzlwToxMKgQ=; b=oDU7y0MnTDZHdzZzqjquFNN8pzLmWlIv5uh5RigGnMJHObfGQCV3PuaMT/QPi5Z9dB OXC2FWOAFpYdjL5nujHU4qn00kaHpPECPAGdp4AZrr0xTTWoC/8kfYs5iP6kgkouyuRp 0rkCv/2cCUwwEFprZ0oWx/SBssacTZl2jzA6++pWXd+JYR8bTUtcYma34UNffVeJzHa1 5AppP00arhKO+CELpozYrNo251U08oYBYhaIskR3rcJZrTULhHXU6nljVPlQu4Uo+t6d duzpiZ6VenGvXXfm1HbW1AZfAUmMZNeX8DHEBjhU/YhHT/UADCsK0CusPfRDvkh6pl+W 0NDg==
X-Received by 10.107.40.76 with SMTP id o73mr36892593ioo.157.1451101441103; Fri, 25 Dec 2015 19:44:01 -0800 (PST)
In-Reply-To <d070aa0d-e80f-4efb-a424-351737ddb2fc@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100878

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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