def <dynamic function name> () syntax ?

Andrew Kuchling akuchlin at mems-exchange.org
Wed Apr 4 13:47:35 EDT 2001


"Bruce Edge" <bedge at troikanetworks.com> writes:
> Can the follwoing be accomplished in Python?
> 
> I want to create a func named "abc":

Just bind the name to 'abc'.
def dummy_name (): 
    ...
 
abc = dummy_name

Or: exec '%s = dummy_name' % name

But such dynamic creation of functions usually strikes me as a flawed
design.  An informal rule of mine is that 'exec' or 'eval' should only
be needed when you're expecting arbitrary expressions, perhaps in a
config file, but exec'ing a single statement or applying eval() to a
single name is usually a sign of programming in some non-Pythonic
style.

--amk




More information about the Python-list mailing list