Question: I wrote a class that lets me pass in a list of variable types, variable names, prompts, and default values. The class creates a wxPython panel, which is displayed in a frame that lets the user set the input values before pressing the calculate button and getting the results back as a plot. I add all of the variables to the class using exec statements. This keeps all of the variables together in one class, and I can refer to them by name.

```
light = Variables( frame , [ ['f','wavelength','Wavelength (nm)',632.8] ,\
                             ['f','n','Index of Refraction',1.0],])

```

Inside the class I create and set the variables with statments like:

```
for variable in self.variable_list:
       var_type,var_text_ctrl,var_name = variable
       if var_type == 'f' :  
           exec( 'self.' + var_name + ' = ' + var_text_ctrl.GetValue() )

```

When I need to use the variables, I can just refer to them by name:

```
 wl = light.wavelength
 n = light.n

```

Then I read on SO that there is rarely a need to use exec in Python. Is there a problem with this approach? Is there a better way to create a class that holds variables that should be grouped together, that you want to be able to edit, and also has the code and wxPython calls for displaying, editing, (and also saving all the variables to a file or reading them back again)?

Curt

Answer: 
You can use the `setattr` function, which takes three arguments: the object, the name of the attribute, and it's value. For example,

```
setattr(self, 'wavelength', wavelength_val)

```

is equivalent to:

```
self.wavelength = wavelength_val

```

So you could do something like this:

```
for variable in self.variable_list:
       var_type,var_text_ctrl,var_name = variable
       if var_type == 'f' :
           setattr(self, var_name, var_text_ctrl.GetValue())

```
The module was removed because it had security issues. Very difficult to provide an environment where any code can be executed in a restricted environment, with all the introspection that Python has.

A better bet is to avoid eval and exec.

A really off-the-wall idea is to use Google App Engine, and let them worry about malicious code.