<p>I'm working on a web application that will return a variable set of modules depending on user input. Each module is a Python class with a constructor that accepts a single parameter and has an '.html' property that contains the output.</p>

<p>Pulling the class dynamically from the global namespace works:</p>

<pre><code>result = globals()[classname](param).html
</code></pre>

<p>And it's certainly more succinct than:</p>

<pre><code>if classname == 'Foo':
    result = Foo(param).html
elif classname == 'Bar':
    ...
</code></pre>

<p>What is considered the best way to write this, stylistically? Are there risks or reasons not to use the global namespace?</p>

First of all, it sounds like you may be reinventing the wheel a little bit... most Python web frameworks (CherryPy/TurboGears is what I know) already include a way to dispatch requests to specific classes based on the contents of the URL, or the user input.
There is nothing
wrong
with the way that you do it, really, but in my experience it tends to indicate some kind of "missing abstraction" in your program.  You're basically relying on the Python interpreter to store a list of the objects you might need, rather than storing it yourself.
So, as a first step, you might want to just make a dictionary of all the classes that you might want to call:
```
```
dispatch = {'Foo': Foo, 'Bar': Bar, 'Bizbaz': Bizbaz}
```
```
Initially, this won't make much of a difference.  But as your web app grows, you may find several advantages: (a) you won't run into namespace clashes, (b) using
```
globals()
```
you may have security issues where an attacker can, in essence, access any global symbol in your program if they can find a way to inject an arbitrary
```
classname
```
into your program, (c) if you ever want to have
```
classname
```
be something other than the actual exact classname, using your own dictionary will be more flexible, (d) you can replace the
```
dispatch
```
dictionary with a more-flexible user-defined class that does database access or something like that if you find the need.
The security issues are particularly salient for a web app.  Doing
```
globals()[variable]
```
where
```
variable
```
is input from a web form is just
asking for trouble
.