Message409055
> 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. |
|
Date |
User |
Action |
Args |
2021-12-23 00:15:29 | eryksun | set | recipients:
+ eryksun, steven.daprano, qpeter |
2021-12-23 00:15:29 | eryksun | set | messageid: <1640218529.25.0.509294318437.issue46153@roundup.psfhosted.org> |
2021-12-23 00:15:29 | eryksun | link | issue46153 messages |
2021-12-23 00:15:29 | eryksun | create | |
|