From 4fbf85d503d24be85263c67ee9f8c48db747210d Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Fri, 31 Jan 2014 10:06:33 -0500 Subject: [PATCH] * Added function_name to obtain the name given to a function. --- py/__init__.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/py/__init__.py b/py/__init__.py index 000aeb3..c3e2ca0 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -30,3 +30,21 @@ def make_unbound_method(method): # Assume this is a static method or user-defined external method # injected into this class. return method + + +def function_name(f): + """Returns the given name of a function (or callable object).""" + try: + # Regular function + return f.func_name + except: + pass + try: + # Instance method + return "%s.%s" % (f.im_class, f.im_func.func_name) + except: + pass + # Callable class instance: + return f.__class__.__name__ + +