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


Groups > comp.lang.python > #81970

Re: About Modifying Globals

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <i@introo.me>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'cpython': 0.05; 'notice,': 0.07; 'variables': 0.07; 'difference,': 0.09; 'function,': 0.09; 'referenced': 0.09; 'semantic': 0.09; 'python': 0.11; 'def': 0.12; 'stored': 0.12; "'b'": 0.16; 'assignment.': 0.16; 'bytecode': 0.16; 'dictionary,': 0.16; 'modified.': 0.16; 'script,': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'mechanism': 0.19; '>>>': 0.22; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'question': 0.24; 'source': 0.25; 'second': 0.26; 'header :In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'function': 0.29; '(which': 0.31; 'that.': 0.31; 'loads': 0.31; 'subject:About': 0.31; 'checking': 0.33; 'received:209.85': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'functions.': 0.36; 'received:209': 0.37; 'performance': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'hope': 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'name': 0.63; 'finally': 0.65; 'regards.': 0.65; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'improvement.': 0.68; 'url:me': 0.69; 'technically': 0.84; 'toy': 0.84; 'treats': 0.84
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=UODXgUWdiISjMaSRZahvr6pxoWpPUMWblCQ/OpIgPTY=; b=UUerbEkXVfddWqkmKOJIy+yz6heRm8iPiowdX4F4zZtWFbaIcX7J17oxyS229piDln 56zpcjOL3l8pLzPsLwR6kJqb1aq0/JwCxpdd5I9H21JYbaDLOeHElZcbxnbt1jPhFmlx XRCf8O+rAjFc+UwONX8QUjMmJ8ZsmjIqIvpyYQNp+mR6nFF1JfIS9Z/JP/CDMyZuQRdn xf0Mkvwnq8lAYNAls61j99uOge4XvirY2hmK8MWBL+6CW2xsejDXMUIBb7wLLyJf1VgU xJMmPx6FJm0DLEdIH5WCwVwdT3WhtPaMenKv7bmFv0Nbav96plbxtkiE3gtRcQuN8cFo kV1g==
X-Gm-Message-State ALoCoQlEqNCHv1Ke7NENA9VvQMZrkF0P/04g9sG5Mdayj1vUFCDZF5nTXhVvnSoVARn3xorP5X5J
X-Received by 10.66.222.100 with SMTP id ql4mr23928956pac.123.1417747994131; Thu, 04 Dec 2014 18:53:14 -0800 (PST)
Date Fri, 5 Dec 2014 10:53:09 +0800
From "shiyao.ma" <i@introo.me>
To LJ <luisjosenovoa@gmail.com>, python-list@python.org
Subject Re: About Modifying Globals
References <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Disposition inline
In-Reply-To <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com>
User-Agent All mail clients suck. This one just sucks less.
X-Info Find me @introom on Freenode. Have beers! :)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.16604.1417748003.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417748003 news.xs4all.nl 2875 [2001:888:2000:d::a6]:43078
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81970

Show key headers only | View raw


On 12/04, LJ wrote:
> Hi All,
>
> I have a quick question regarding the modification of global variables within functions. To illustrate, consider the following toy example:
>
> a={"1": set()}
> b=9
>
> def gt(l):
>    a["1"] = a["1"] | set([l])
>
> When calling this last function and checking the a dictionary, I get:
>
> >>> gt(5)
> >>> a
> {"1": set([5])}
>
>
> The set in the dictionary was modified. The question is, why isn't it necessary to declare a as global within the gt function, as apposed to a case like
>
> def gt2(l):
>    b=b+l
>
> where I need to declare b as global within the function to avoid:
>
> UnboundLocalError: local variable 'b' referenced before assignment.

Well. To understand the difference, one first has to know the mechanism of CPython.
When you have a script, CPython will first parse it, and then generate bytecode representation, and then finally execute it.
If you take a look at the bytecode of both your source code, you will notice, that.

In the first snippet, there is something like LOAD_NAME, which loads the global name a.
However, in the second snippet, there would be a LOAD_FAST, which loads the local name b (which is, technically speaking, stored in the PyFrameObj).

The reason Python treats it differently, is, IIUC, for better semantic meaning, and also, for huge performance improvement.


Hope that explains.


Regards.

-- 
Shiyao Ma
http://introo.me

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

About Modifying Globals LJ <luisjosenovoa@gmail.com> - 2014-12-04 12:09 -0800
  Re: About Modifying Globals Chris Angelico <rosuav@gmail.com> - 2014-12-05 07:22 +1100
  Re: About Modifying Globals Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-04 13:21 -0700
  Re: About Modifying Globals Dave Angel <davea@davea.name> - 2014-12-04 18:54 -0500
  Re: About Modifying Globals Chris Angelico <rosuav@gmail.com> - 2014-12-05 10:58 +1100
  Re: About Modifying Globals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-05 13:09 +1100
    The binding operator, and what gets bound to what (was: About Modifying Globals) Ben Finney <ben+python@benfinney.id.au> - 2014-12-05 16:36 +1100
      Re: The binding operator, and what gets bound to what (was: About Modifying Globals) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-05 20:53 +1100
        Re: The binding operator, and what gets bound to what (was: About Modifying Globals) Chris Angelico <rosuav@gmail.com> - 2014-12-05 21:03 +1100
          Re: The binding operator, and what gets bound to what (was: About Modifying Globals) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-05 23:26 +1100
            Re: The binding operator, and what gets bound to what (was: About Modifying Globals) Chris Angelico <rosuav@gmail.com> - 2014-12-05 23:42 +1100
        Re: The binding operator, and what gets bound to what Ned Batchelder <ned@nedbatchelder.com> - 2014-12-05 07:06 -0500
          Re: The binding operator, and what gets bound to what Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-05 23:34 +1100
            Re: The binding operator, and what gets bound to what Chris Angelico <rosuav@gmail.com> - 2014-12-05 23:48 +1100
            Re: The binding operator, and what gets bound to what Ned Batchelder <ned@nedbatchelder.com> - 2014-12-05 07:56 -0500
  Re: About Modifying Globals "shiyao.ma" <i@introo.me> - 2014-12-05 10:53 +0800

csiph-web