This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients eryksun, qpeter, steven.daprano
Date 2021-12-23.00:15:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1640218529.25.0.509294318437.issue46153@roundup.psfhosted.org>
In-reply-to
Content
> If exec gets two separate objects as globals and locals, 
> the code will be executed as if it were embedded in a 
> class definition.

That's a misleading comparison because a class definition intentionally supports nonlocal closures, which exec() doesn't support and shouldn't support. For example:

    a = 1

    def f():
        a = 2
        class C:
            print(a)

    def g():
        a = 2
        class C:
            nonlocal a
            a = 3
        print(a)

    >>> f()
    2
    >>> g()
    3

exec() executes as module code. Using separate globals and locals mappings doesn't magically change how the code is compiled and executed to make it equivalent to a class definition. To understand the case of separate globals and locals, just remember that assigning to a variable by default makes it a local variable, unless it's declared as a global. Also, class and function definitions are implicitly an assignment, which by default will be local.
History
Date User Action Args
2021-12-23 00:15:29eryksunsetrecipients: + eryksun, steven.daprano, qpeter
2021-12-23 00:15:29eryksunsetmessageid: <1640218529.25.0.509294318437.issue46153@roundup.psfhosted.org>
2021-12-23 00:15:29eryksunlinkissue46153 messages
2021-12-23 00:15:29eryksuncreate