Invoking top-level function by name in Python
<p>How can I invoke a top-level function by name? For example,</p>

<pre><code>#!/usr/bin/env python

import sys

def foo():
  print 'foo'

def bar():
  print 'bar'

# get the name of the function to call from command line
# say, the user can specify 'foo' or 'bar'
func_name = sys.argv[1]

# how do I invoke the function by func_name?
getattr(__main__, func_name)  # NameError: global name '__main__' is not defined
</code></pre>
