python - Can one function have multiple names? -
i'm developing bot on python (2.7, 3.4). defined 30+ dynamic functions used based on bot commands. while development, since not functions done, have define them empty functions (if not define code won't run) this:
def c_about(): return def c_events(): return def c_currentlocation(): return etc. many dummy functions.
question:
it somehow possible in python define same function multiple names?
this:
def c_about(), c_events(), c_currentlocation(): return
functions not intern (i.e., automatically share multiple references same immutable object), can share same name:
>>> def a(): pass ... >>> <function @ 0x101c892a8> >>> def b(): pass ... >>> b <function b @ 0x101c89320> >>> c=a >>> c <function @ 0x101c892a8> # note physical address same 'a' so can do:
>>> c=d=e=f=g=a >>> e <function @ 0x101c892a8> for case of functions not yet defined, can use try/catch block catching either nameerror:
def default(): print "default called" try: not_defined() except nameerror: default() or use dict of funcs , catch keyerror:
funcs={"default": default} try: funcs['not_defined']() except keyerror: funcs['default']() or, can funcs.get(not_defined, default)() if prefer syntax dict of funcs.