Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81944
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@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; 'example:': 0.03; 'explicitly': 0.05; 'assign': 0.07; 'assignment': 0.07; 'compiler': 0.07; 'modify': 0.07; 'variables': 0.07; 'function,': 0.09; 'referenced': 0.09; 'variable,': 0.09; 'def': 0.12; "'b'": 0.16; '1:09': 0.16; 'assignment.': 0.16; 'dictionary,': 0.16; 'modified.': 0.16; 'url:faq': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'thu,': 0.19; '>>>': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; 'question': 0.24; '>': 0.26; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'default,': 0.31; 'subject:About': 0.31; 'url:python': 0.33; 'checking': 0.33; "can't": 0.35; 'knows': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'functions.': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'skip:\xc2 10': 0.60; 'url:3': 0.61; 'first': 0.61; 'within': 0.65; 'toy': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=6QmMIWnPKA0IWm8QBXJGQk6ZxnLmsG8IPJYqN3b8aYk=; b=uLyH7qhPVsPcLSqsVCFRQ8w4ahiU858JVvMCz7MpQzV9yfQVs/wTXXzoPklvoFPAn/ 9wIRBYoaPmHU0rHnDNTRtPf/G2WrsHIJdniMnFyWzTLaky7I+ZGPRsmMigwIgmhngHFS XcQk6WktW2gZASmh6WOOGMHNlyZep3Rj4JNyj+l07rXgx/SZEINeS3CmHWv/v4kGIT5t lpuBNpUO5SJZPhPGKWCSF0+cAuvROUdttQU5qlAfT8UskxSinsgGGIjr4IDfNkS7E7By gSM9Jr9aD1fApbqjrDK+y8Wh9XbOO3PzD2YlL7B0f6jCHgrCmAJZAfeLq3r7ajDhohy9 32lQ== |
| X-Received | by 10.68.107.162 with SMTP id hd2mr28983807pbb.145.1417724540292; Thu, 04 Dec 2014 12:22:20 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com> |
| References | <aa74bd64-0e11-492d-ae2a-4b032a0408f9@googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Thu, 4 Dec 2014 13:21:39 -0700 |
| Subject | Re: About Modifying Globals |
| To | Python <python-list@python.org> |
| Content-Type | multipart/alternative; boundary=047d7b6da6d49d1311050969b7c5 |
| 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.16584.1417724544.18130.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417724544 news.xs4all.nl 2904 [2001:888:2000:d::a6]:38491 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81944 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Thu, Dec 4, 2014 at 1:09 PM, LJ <luisjosenovoa@gmail.com> 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.
https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python
In the first case, you never assign to a; you only modify it. Since it's
never assigned locally the compiler knows it can't be a local variable, so
it automatically makes it a global.
In the second case, the assignment to b causes the compiler to treat the
variable as local by default, so it needs to be explicitly marked as global.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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