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


Groups > comp.lang.python > #81962

Re: About Modifying Globals

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
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; 'example:': 0.03; 'assignment': 0.07; 'compiler': 0.07; 'variables': 0.07; "'a'": 0.09; 'function,': 0.09; 'namespace': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; "'b'": 0.16; 'assignment.': 0.16; 'assignments.': 0.16; 'bind': 0.16; 'confuse': 0.16; 'dictionary,': 0.16; 'immutable,': 0.16; 'modified.': 0.16; 'statement.': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'normally': 0.19; 'seems': 0.21; 'example': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'sort': 0.25; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; '>>>>': 0.31; 'names.': 0.31; 'object.': 0.31; 'occurs': 0.31; 'subject:About': 0.31; 'another': 0.32; 'checking': 0.33; 'not.': 0.33; 'could': 0.34; 'objects': 0.35; 'but': 0.35; 'functions.': 0.36; 'module.': 0.36; 'list.': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'name': 0.63; 'within': 0.65; 'received:74.208': 0.68; 'toy': 0.84
Date Thu, 04 Dec 2014 18:54:26 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: About Modifying Globals
References <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com>
In-Reply-To <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:54MmgZDWae2KZcK1lsNRp2QABE+7SZ1glka+f9BQMQN Owss6i268OvzTFnOM8bggwGJm5S7hMK8x6QenOI9cocVkr0XQP xiwn8vveZnpDgebmUT1aXD8vYk7e05mswTFullKKW8jmNoQg5J v/AhPXUeCdjYVgcU4Gtnhybkm+wKIjZXMoZUkuUDw2uYjK6e2j Y6VM3QFhkOJhwrIOguAEsPn52TwTcrme9zBntC71ZTuhlzNcnz 0Tv4vLEO4C6L4rkCSUKHuKYAuN/UDXOfI4RbSwxaUDG6fZr3Uu xqeDbWgTtdpxWS1eK5TKzjWgrNOrqaPgITKoFm3Lv+836JXuq4 aWwgO7XCiL7ud1c+prII=
X-UI-Out-Filterresults notjunk:1;
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.16599.1417737288.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417737288 news.xs4all.nl 2923 [2001:888:2000:d::a6]:40556
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81962

Show key headers only | View raw


On 12/04/2014 03:09 PM, 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.
>

The reason this sort of thing seems to confuse lots of people is that we 
insist on calling these things assignments.  The thing that's global is 
the name 'a'.  It's global because it's in the global() namespace of 
some module.

The data that's "assigned" to it is an object.  You bind the object to 
the name 'a' with an assignment statement.  If that object is immutable, 
then the only way to change 'a' is to do another assignment.

But if the object is mutable, as a set is, then it can change as much as 
you like without rebinding the name.

A given object may have any number of names bound to it, including no 
names.  An example of that could be objects that are referenced only in 
some list.

Python doesn't have declarations, so when a function is compiled, the 
compiler has to infer what names are to be local and what are not.  The 
rule it normally uses is roughly based on whether an assignment occurs 
somewhere inside the function.


-- 
DaveA

Back to comp.lang.python | Previous | Next — Previous in thread | Next 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