I\'m developing a bot on Python (2.7, 3.4). I defined a about 30+ dynamic functions which to be used based on bot commands. While development, since not all functions are done,
Functions do not intern in Python (i.e., automatically share multiple references to the same immutable object), but can share the same name:
>>> def a(): pass
...
>>> a
>>> def b(): pass
...
>>> b
>>> c=a
>>> c
# note the physical address is the same as 'a'
So clearly you can do:
>>> c=d=e=f=g=a
>>> e
For the case of functions not yet defined, you can use a try/catch
block by catching either a NameError
:
def default():
print "default called"
try:
not_defined()
except NameError:
default()
Or use a dict of funcs and catch the KeyError
:
funcs={"default": default}
try:
funcs['not_defined']()
except KeyError:
funcs['default']()
Or, you can do funcs.get(not_defined, default)()
if you prefer that syntax with a dict of funcs.