[Tutor] executing dynamic code with exec?

Chris Hare chare at labr.net
Fri Dec 2 03:52:09 CET 2011


I have this code chunk:

tables = ["Farm", "Animals", "AnimalTypes","Users","Roles","Capabilities","Pedigrees","ChipMaker","Owner","Providers","RegistryL"]
for x in tables:
	cmd = "self.cb" + x + "Read = IntVar()"
	exec cmd in locals(), globals()

When the code is executed, I get the following error:

  File "z.py", line 4398
    exec cmd in locals(), globals()
SyntaxError: function 'showRoles' uses import * and bare exec, which are illegal because it contains a nested function with free variables

What I am trying to do is create a set of variables based upon the table names in the table variables.  I have similar code which dynamically creates check buttons and the associated grid.  But I suspect those won't work either.

What have I got wrong?

Thanks!

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111201/d311e538/attachment-0001.html>


More information about the Tutor mailing list