Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: exec inside functions in Python 3 Date: Wed, 23 Mar 2016 00:31:45 +1100 Lines: 40 Message-ID: References: <56f14156$0$1619$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 6qKfnMw3o7ycuCHLjkUcPQQ4qi9cAJMJwRgEzX74DnNw== Return-Path: 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; 'subject:Python': 0.05; 'cc:addr:python-list': 0.09; '22,': 0.09; 'here?': 0.09; 'propagate': 0.09; 'python': 0.10; 'def': 0.13; '2.7:': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mutated': 0.16; 'one-way': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'test()': 0.16; 'test():': 0.16; 'variables;': 0.16; 'wrote:': 0.16; 'changes': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; 'exec': 0.22; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'back.': 0.27; 'function': 0.28; 'idea': 0.28; 'other,': 0.29; 'anyone': 0.32; 'maybe': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'happened': 0.35; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'is.': 0.63; 'mar': 0.65; 'chrisa': 0.84; "it'd": 0.84; 'locals': 0.84; 'spam)': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=GN8NW3K5CvMPZhDIMz1wPA/SREYE5nYggtC9yIVqBbc=; b=l0XIqDRmRDfu1ha8AF4pyyTNZEl9t2IE1pOIaiD8bN/8U9KekEU9k1ih4oIrQqAQhY mRbyvHexz9//WQeo23Hgo3MRK2gY7yyg6ZESk115Y40AODYbHiwL32uASLICjl2xppFq FXSWY2hi5fq1IP1rNjuZnO+7g8Pv3lC4yH7MOUMXuYoV4ECcuzOSmS2iZLJi7EHNyAub CxOGWZ6EOSFFxa+2sdunQvlY+eEQOgAh49ePKfQMGCPLSRdQnCR5OROJH0zGyplJBfxn QB64Bw378xYg0kMlZzUDBLkU0TT/aKWcTgie8SAu0GAJ5q3nt8SAhMzNCL9MpHViUaSu ee2g== 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:date :message-id:subject:from:cc; bh=GN8NW3K5CvMPZhDIMz1wPA/SREYE5nYggtC9yIVqBbc=; b=e3SpMahWX8YAwBlPReXbJmKpkN0QpCNem+drzj5JBG+ije/IgbPwCINP4EtIAYDnCC d4NFwmoZvYmkQP82mXlYX6JtKExMJDduxAbAktV+6+kcxfs4qNsUS0jriv5QpWFFXpWF +kgCcYZPDebXJfMuZihw8rAq3I5AZk041sxhzd7x4Rt1h7QoOsyUMEoZlBXTYuvHBc2H kInw7BW4uYNC7YWYG7Qw4nCP6sxXENHr8RhHU5rKo3GvwhoTFWRCqBTsECjHF1zM5d+L xOXaTD1pXd79sNB0lqchMPw8nUAHbwS62dCnKZ3OqJmTlTpZQLSFYE+p2gfkcaHlb1e5 y7GA== X-Gm-Message-State: AD7BkJL/499KTcJWVN0Adz23D3OTaca5mGAH9jx1mrXgtQPqYW+EM3izMG0qmx1GHvwQI/vnlV30Z9e3LYvIww== X-Received: by 10.25.83.138 with SMTP id h132mr13140421lfb.7.1458653505925; Tue, 22 Mar 2016 06:31:45 -0700 (PDT) In-Reply-To: <56f14156$0$1619$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105483 On Tue, Mar 22, 2016 at 11:57 PM, Steven D'Aprano wrote: > Anyone have any idea what is going on here? > > > def test(): > spam = 1 > exec("spam = 2; print('inside exec: %d' % spam)") > print('outside exec: %d' % spam) > > > In Python 2.7: > > py> test() > inside exec: 2 > outside exec: 2 > > > > In Python 3.4: > > outside exec: 1 > py> test() > inside exec: 2 > outside exec: 1 > > > > What happened to spam? In Python 2, exec is magical. In Python 3, it's a function like any other, so it doesn't have access to local variables; what it gets is locals(), which is a one-way representation of current locals - changes don't propagate back. It'd maybe be nice to be able to tell Python to compile a function with a "real locals dictionary", which would then be mutated by locals() changes (as locals() would simply return it as-is). That'd fix this "problem", if problem it indeed is. ChrisA