Skip to content

Instantly share code, notes, and snippets.

@geekKeen
Last active January 9, 2018 02:43
Show Gist options
  • Save geekKeen/0f97c6963a8c60e3ccc310363c44ca2a to your computer and use it in GitHub Desktop.
Save geekKeen/0f97c6963a8c60e3ccc310363c44ca2a to your computer and use it in GitHub Desktop.
inspection func
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 ')
@geekKeen
Copy link
Author

geekKeen commented Jan 9, 2018

callable:

  1. buildin function
  2. user defined function
  3. lambda
  4. class
  5. class call class instance
  6. class method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment