Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: the global keyword: Date: Fri, 24 Jun 2016 02:19:57 +0000 Lines: 41 Message-ID: References: <646e9ff9-b5a1-4403-b79a-694397e34b0b@googlegroups.com> <1466529321.3842871.644317377.08527069@webmail.messagingengine.com> <195658b2-e1b2-4c91-8ff8-bd58379e700f@googlegroups.com> <576c863a$0$1598$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de JuVSppAJgB1TPxTYMPmwfgLuP38SVm1vQDJytzXSfMaQ== Return-Path: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <646e9ff9-b5a1-4403-b79a-694397e34b0b@googlegroups.com> <1466529321.3842871.644317377.08527069@webmail.messagingengine.com> <195658b2-e1b2-4c91-8ff8-bd58379e700f@googlegroups.com> <576c863a$0$1598$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:110447 On Fri, Jun 24, 2016 at 1:00 AM, Steven D'Aprano 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.