You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2008-11-21.21:53:43.060>created_at=<Date2008-11-21.20:33:45.006>labels= []
title='Cannot declare multiple classes via exec when inside a function.'updated_at=<Date2008-11-21.21:53:43.058>user='https://bugs.python.org/kfitch'
<type 'exceptions.NameError'>: name 'foo' is not defined
I don't fully understand why the version in the function doesn't work,
but I suspect it is a bug related to scoping, since foo is really
doExec.foo (I think).
This is with python 2.5.2 under Linux (Ubuntu 8.04)
I agree that it is confusing; in short: a bare exec() does not play well with closures;
This is a consequence of the python execution model, and is unlikely to change.
Here, the class 'foo' is stored in the function's local variables.
But the execution of the body of the 'bar' class searches names in its local scope (the
class body) and the global scope (the module level), and misses the function's
locals...
I strongly suggest to avoid pure exec() statements; always specify a global and/or a
local dictionary.
In your case, the following works:
defdoExec(text):
d= {} # or: d = dict(globals())exectextindprintd.keys()
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: