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


Groups > comp.lang.python > #100886

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

Path csiph.com!news.swapon.de!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 20:49:47 +1100
Lines 86
Message-ID <mailman.19.1451123395.11925.python-list@python.org> (permalink)
References <d070aa0d-e80f-4efb-a424-351737ddb2fc@googlegroups.com> <mailman.17.1451101449.11925.python-list@python.org> <701dd0e6-a9c1-4aa9-a3a2-6607cd3f3759@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de GYMbeiUOxw8g3xzeoXmmvAzwXZfTn6MtBa+Y5j0SYHyQ==
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; 'json': 0.05; 'python3': 0.05; 'sys': 0.05; '(so': 0.07; 'assignment': 0.07; 'default.': 0.07; 'sys,': 0.07; 'works.': 0.07; 'cc:addr:python-list': 0.09; 'assigning': 0.09; 'augmented': 0.09; 'before.': 0.09; 'closest': 0.09; 'created,': 0.09; 'err:': 0.09; 'files:': 0.09; 'fp:': 0.09; 'statements': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'interpreter': 0.15; '"an': 0.16; '"file': 0.16; "'except'": 0.16; '(more': 0.16; '(normally': 0.16; 'assignment.': 0.16; 'assigns': 0.16; 'called,': 0.16; 'clause.': 0.16; 'constructs': 0.16; 'declaration,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator,': 0.16; 'magic': 0.16; "module's": 0.16; 'open(file)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sorts': 0.16; 'statement.': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'basically': 0.18; 'flexibility': 0.18; 'instance,': 0.18; 'nested': 0.18; 'try:': 0.18; 'creates': 0.18; 'language': 0.19; '>>>': 0.20; 'load': 0.20; 'changes': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; 'do.': 0.22; '3.x': 0.22; 'function,': 0.22; 'os,': 0.22; 'python"': 0.22; 'seems': 0.23; '(like': 0.23; 'dec': 0.23; "python's": 0.23; 'sat,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'linux': 0.26; 'rest': 0.26; 'equivalent': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; 'cool': 0.27; 'defining': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'block,': 0.29; 'declared': 0.29; 'follows': 0.29; 'if,': 0.29; 'value)': 0.29; 'there.': 0.30; 'too.': 0.30; 'normally': 0.30; "i'd": 0.31; 'rules': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'statement': 0.32; 'file': 0.34; 'except': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'doing': 0.38; 'received:209': 0.38; 'stuff': 0.38; 'anything': 0.38; 'thank': 0.38; 'end': 0.39; 'why': 0.39; 'data': 0.39; 'whatever': 0.39; 'does': 0.39; 'where': 0.40; 'mark': 0.40; 'still': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'more': 0.63; 'within': 0.64; 'statement,': 0.66; '26,': 0.72; 'special': 0.73; "'for'": 0.84; "'with'": 0.84; '11:': 0.84; 'chrisa': 0.84; 'etc,': 0.84; 'reminds': 0.84; 'to:none': 0.91; 'this!': 0.93; 'hand,': 0.97
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:content-transfer-encoding; bh=eElI5d01yUdRKxjUe7mYmZ7yJvbS++Txlzu4BgJOgD8=; b=G5HYdmTAG5SzQDJ2YAkoWw+GXjha1P3W1sKpv+ome62PcU+drSNA0HK1RQncaXtwa+ pNUa3vWoRWTvw3ROycnUlgsy3lxMVJYq5wKAVeyUO4d8MuNfvsB5sjzsL2YvjXc6ueoC A4FQ/NdBhQsIXLv3zqJ0PBxRLQDWh9cFA+3JDrauXVQE7/eqQfXWVPOl6BR2GygCof2y j7TU1Iv7IzPMmzMUhmAmKoehjxGlkMELGx4IJpoNOdld13mNG7VX8CNOtWSEwcnFX1N5 Lpu6fOWaMgFbrBTfJ0zMxHtucViP8B86ZIi60hOP8irC1NRQd1ss6rGrhjDifoSgrekU e4Bw==
X-Received by 10.50.28.19 with SMTP id x19mr44670704igg.92.1451123387469; Sat, 26 Dec 2015 01:49:47 -0800 (PST)
In-Reply-To <701dd0e6-a9c1-4aa9-a3a2-6607cd3f3759@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:100886

Show key headers only | View raw


On Sat, Dec 26, 2015 at 8:07 PM,  <jfong@ms4.hinet.net> 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 name X in the current local
> scope, by default. If X is declared global within the function, the assignment creates or changes the name X in the enclosing module's scope instead. If, on the other hand, X is declared  nonlocal within the function in 3.X (only), the assignment changes the name X in the closest enclosing function's local scope."
>

Yep! That's an accurate description of how assignment works.

One of the cool things about Python is that there are all sorts of
things that work _exactly_ the same way. Every one of these statements
is a form of assignment:

import json  # 1
from sys import argv  # 2
def testfiles(files): # 3, 4
    failures = 0 # 5
    for file in files:  # 6
        with open(file) as fp: # 7
            try: data = json.load(fp) # 8
            except JSONDecodeError as err: # 9
                failures += 1 # 10
    return failures
count = testfiles(argv) # 11

Okay, every one except the 'return' statement. :)

1: "import x" is basically the same as "x = __import__('x')".
2: "from x import y" is basically "y = __import__('x').y" (more or
less). This grabs "sys.argv" and assigns it to the name "argv".
3: Defining a function constructs a new function object and assigns it
to the name. This is like doing "testfiles = <magic>".
4: As the function gets called, a new scope is created, and inside
that scope, the interpreter does the equivalent of "files = <magic>",
where the magic snags a reference to whatever was used as the argument
(so this is basically "files = argv").
5: That's straight-forward assignment, right there.
6: A 'for' loop grabs an iterator, then repeatedly does "file =
next(iterator)" until there's nothing more to do.
7: A 'with' statement does some work, and then does "fp = <result of
that work>" if it has an 'as' clause.
8: Another straight-forward assignment, because I couldn't think of
anything better to use. (Normally you'd lay this out with 'try:' on a
separate line, but then I'd have had a line without any assignment at
all.)
9: Like a 'with' statement, 'except' assigns to its name. There's a
special case here, in that it also does 'del err' at the end of the
except block, to clean stuff up; but it's still just assignment.
10: Augmented assignment is assignment too. x+=1, x-=1, x*=1, etc, are
all assigning to x.
11: Another normal assignment, because otherwise the rest of the work
is pointless. :)

Every one of these follows the standard rules for assignment. For
instance, you could create a function that does a top-level import:

$ python3
Python 3.6.0a0 (default:6e114c4023f5, Dec 20 2015, 19:15:28)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def do_imports():
...     global os, sys, json
...     import os, sys, json
...
>>> do_imports()
>>> json
<module 'json' from '/usr/local/lib/python3.6/json/__init__.py'>

You wouldn't normally do this for standard modules like 'os' and
'sys', but if you have something huge to load up (like pandas, or
oauth2client), it might be convenient to use them globally, but load
them conditionally. Since 'import' is a statement, not a declaration,
you can do this!

Python's flexibility and simplicity are a huge part of why I love the
language so much.

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