Skip to content

Instantly share code, notes, and snippets.

@bencharb
Last active August 29, 2015 14:15
Show Gist options
  • Save bencharb/627367cd0ad07e968b45 to your computer and use it in GitHub Desktop.
Save bencharb/627367cd0ad07e968b45 to your computer and use it in GitHub Desktop.
Get public methods from class
#!/bin/python
import operator
import inspect
_get_method_name = operator.itemgetter(0)
def get_private_methods(cls):
def is_private(attr):
return inspect.isbuiltin(attr) or inspect.ismethoddescriptor(attr)
return map(_get_method_name, inspect.getmembers(cls, is_private))
def filter_attr_by_pattern(attr):
return not attr.startswith('_')
def get_public_methods(cls, filter_underscore_prefix=True):
public = set(map(_get_method_name, inspect.getmembers(cls, inspect.isroutine))).difference(get_private_methods(cls))
if filter_underscore_prefix:
public = filter(filter_attr_by_pattern, public)
return public
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment