Last active
January 9, 2018 02:43
-
-
Save geekKeen/0f97c6963a8c60e3ccc310363c44ca2a to your computer and use it in GitHub Desktop.
inspection func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_callable_name(func): | |
if hasattr(func, '__qualname__'): | |
return func.__qualname__ | |
# get func's class | |
f_self = getattr(func, '__self__', None) or getattr(func, 'im_self', None) | |
if f_self and hasattr(func, '__name__'): | |
# bound method class | |
f_class = f_self if isinstance(f_self, type) else f_self.__calss__ | |
else: | |
# class unbound method | |
f_class = getattr(func, 'im_class', None) | |
# class methods, bound and unbound methods | |
if f_class and hasattr(func, '__name__'): | |
return '%s.%s' % (f_class.__name__, func.__name__) | |
# class or class instance | |
if hasattr(func, '__call__'): | |
# class | |
if hasattr(func, '__name__'): | |
return func.__name_ | |
# class instance | |
return func.__class__.__name__ | |
raise ValueError('Unable to determine a name for ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
callable: