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


Groups > comp.lang.python > #110447

Re: the global keyword:

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: the global keyword:
Date Fri, 24 Jun 2016 02:19:57 +0000
Lines 41
Message-ID <mailman.89.1466734840.11516.python-list@python.org> (permalink)
References <edb72d55-3a44-45f8-8122-b86d62e837c3@googlegroups.com> <njkbu8$e4c$1@dont-email.me> <646e9ff9-b5a1-4403-b79a-694397e34b0b@googlegroups.com> <1466529321.3842871.644317377.08527069@webmail.messagingengine.com> <mailman.10.1466529325.11516.python-list@python.org> <195658b2-e1b2-4c91-8ff8-bd58379e700f@googlegroups.com> <nkchrb$b20$1@dont-email.me> <cf500e11-7b28-42bc-a62f-670eb1fc48bf@googlegroups.com> <nkdlg4$52e$1@dont-email.me> <576c863a$0$1598$c3e8da3$5496439d@news.astraweb.com> <CACL+1audUdP3EvafsAY_NZVdikn46x8bE1tSZNR9E3GRUTVnzA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de JuVSppAJgB1TPxTYMPmwfgLuP38SVm1vQDJytzXSfMaQ==
Return-Path <eryksun@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; 'received:209.85.223': 0.03; 'cpython': 0.05; 'sys': 0.05; 'builtin': 0.07; 'builtins': 0.09; 'exceptions,': 0.09; 'executes': 0.09; 'global,': 0.09; 'literal': 0.09; 'example:': 0.10; '"_"': 0.16; '2016': 0.16; '24,': 0.16; 'closures': 0.16; 'constants': 0.16; 'fallback': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'statement)': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'variable': 0.18; 'language': 0.19; '>>>': 0.20; 'function,': 0.22; 'am,': 0.23; '(or': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'example': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; '(used': 0.29; 'array': 0.29; 'objects': 0.29; 'code': 0.30; 'operations': 0.31; 'possibly': 0.32; 'statement': 0.32; 'class': 0.33; "d'aprano": 0.33; 'foo': 0.33; 'loading': 0.33; 'steven': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'mapping': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; '1:00': 0.84; 'dict.': 0.84; 'locals': 0.84; 'respectively': 0.84; 'snapshot': 0.84; 'subject::': 0.85
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; bh=iI5yGRnAvtjLED1fOcWP5wMoEsq8bLEMSfEmShyLt7I=; b=X8UvHkab7+xqkaLXLRAC/ZCzqkmSKN9sfdGPVGWaXAVmvulVZtI2dUb7HWc4U4uqdT n7FUOUrF4NGJMjQf8XSZnap6kmgqjYZHWOsSG20cobMclsbKVHhskDCXuL1lPDs03jr9 y3cxkw6vICwyp2pRaSwOp5jE34V/DHqwu2yOVIzmIsEbU30JSwx6CXRRrZkhhtZvb+86 ODZTTUnC4ZihUbnZoHEUR4eiRhAjbXZX3Pp3rLHuWR7CyKdGEAkQqCyNDBYlX0apOXcw 5qg9gYEg/CwzAZrSM5t6k4BkQaKFeOgu2hYanQgCVYVPCJDuk17qrS9AggrVY5RcdbDh fwvg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=iI5yGRnAvtjLED1fOcWP5wMoEsq8bLEMSfEmShyLt7I=; b=Mw+VYdt6om94tJGyOiZ6kz17pbmUQXfMf7plg26jOkz4NYXgKLFhNQo6OlcVgAt/uf oCw0nJByZHfDFuqalc9rG4X128Y2ghGAWISvYs9pbjmVMWHudG6AuoeCWjc/9LjwsYK5 45n1N1V9On4cZbuq4/neeqwdwOFhiylKZ67jWLOAnT6xSere/BUZ7WB3p+l8FbjSySaM 8EHQJWFshk5yKgv9TUMHvcMbmuab2F6yqm4BhhYFpJfuP2aUIW+hxPLdTpiNVfXw3HCg qZccYJrIRGOJrZEGlJ2K29i+CAJOE3jhOfmSqHW4B2vU9rsbNXXp9e2EQAbvbfT0GHKf I6JQ==
X-Gm-Message-State ALyK8tIlyPoqiyFK73L3pXB/aZFc/eH0wkL9dRpUujBhOB4wR387Gg6TJIXncaK3nmcn3VFkMvraIfWMEatJ3w==
X-Received by 10.107.161.71 with SMTP id k68mr4092580ioe.103.1466734837053; Thu, 23 Jun 2016 19:20:37 -0700 (PDT)
In-Reply-To <576c863a$0$1598$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CACL+1audUdP3EvafsAY_NZVdikn46x8bE1tSZNR9E3GRUTVnzA@mail.gmail.com>
X-Mailman-Original-References <edb72d55-3a44-45f8-8122-b86d62e837c3@googlegroups.com> <njkbu8$e4c$1@dont-email.me> <646e9ff9-b5a1-4403-b79a-694397e34b0b@googlegroups.com> <1466529321.3842871.644317377.08527069@webmail.messagingengine.com> <mailman.10.1466529325.11516.python-list@python.org> <195658b2-e1b2-4c91-8ff8-bd58379e700f@googlegroups.com> <nkchrb$b20$1@dont-email.me> <cf500e11-7b28-42bc-a62f-670eb1fc48bf@googlegroups.com> <nkdlg4$52e$1@dont-email.me> <576c863a$0$1598$c3e8da3$5496439d@news.astraweb.com>
Xref csiph.com comp.lang.python:110447

Show key headers only | View raw


On Fri, Jun 24, 2016 at 1:00 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> To be precise, for the builtins module specifically, you can *read* the
> value of the variable using just x, but you cannot *assign* to it unless
> you use the fully-qualified name builtins.x.

FWIW, you can execute code in any dict:

    >>> import builtins
    >>> exec(r'''
    ... foo = 'bar'
    ... ''', vars(builtins))
    >>> builtins.foo
    'bar'

The frame for an unoptimized CPython code object (used for modules and
the class statement) uses f_builtins, f_globals, and f_locals dicts
(or possibly a mapping in the case of f_locals) respectively for its
builtin, global, and local scopes. The frame for an optimized code
object uses a fast locals array instead of f_locals, in which case the
locals() function stores a snapshot of the locals to the f_locals
dict. A frame may also have a freevars array, which has cell objects
for closures (i.e. the nonlocal scope). For optimized code (i.e. a
function, but not a class body), locals() also includes a snapshot of
nonlocals, even though this doesn't make literal sense.

The above example executes code with all 3 scopes set to the same
dict. For example:

    >>> exec(r'''
    ... import sys
    ... f = sys._getframe()
    ... print(f.f_builtins is f.f_globals is f.f_locals)
    ... ''', vars(builtins))
    True

The CPython VM doesn't have an operation to store to the builtin
scope, since the language doesn't have a statement such as "builtin"
to declare a variable in this scope. The builtin scope is used as a
fallback for operations such as LOAD_NAME and LOAD_GLOBAL -- for
loading constants such as built-in functions, exceptions, __debug__,
and also the "_" attribute that gets updated by sys.displayhook.

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


Thread

the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 16:44 -0700
  Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 19:50 -0400
    Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 17:12 -0700
      Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 21:27 -0400
    Re: the global keyword: Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-11 20:15 -0700
      Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-12 14:46 -0400
  Re: the global keyword: MRAB <python@mrabarnett.plus.com> - 2016-06-12 01:09 +0100
    Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 17:13 -0700
      Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-11 20:41 -0700
  Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 21:26 -0400
    Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-12 13:38 +1000
      Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-11 20:48 -0700
        Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-12 11:31 -0700
  Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-12 20:07 +0100
    Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-12 12:25 -0700
      Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-12 22:10 +0100
        Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-19 16:35 +0200
          Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-19 22:20 +0100
            Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-20 13:14 +0200
              Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-20 22:15 +1000
                Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-20 15:04 +0200
                Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-20 09:29 -0400
                Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-21 00:53 +1000
                Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-21 10:08 +0200
                Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-21 11:41 +0100
                Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-21 13:01 +0200
        Re: the global keyword: Joel Goldstick <joel.goldstick@gmail.com> - 2016-06-19 13:16 -0400
          Re: the global keyword: Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-19 22:50 -0700
          Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-20 11:21 +0100
            Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-20 22:03 +1000
    Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 10:04 -0700
      Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-21 13:15 -0400
        Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 15:20 -0700
          Re: the global keyword: Chris Angelico <rosuav@gmail.com> - 2016-06-22 08:32 +1000
            Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 16:43 -0700
          Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-22 00:15 +0100
            Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 17:24 -0700
              Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-22 10:24 +0100
                Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-24 11:00 +1000
                Re: the global keyword: Chris Angelico <rosuav@gmail.com> - 2016-06-24 11:19 +1000
                Re: the global keyword: eryk sun <eryksun@gmail.com> - 2016-06-24 02:19 +0000
          Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 16:50 -0700

csiph-web