Skip to content

Instantly share code, notes, and snippets.

@eferro
Created July 28, 2012 22:55
Show Gist options
  • Save eferro/3195102 to your computer and use it in GitHub Desktop.
Save eferro/3195102 to your computer and use it in GitHub Desktop.
Get classes, functions, etc... using instrospection (inspect module)
#!/usr/bin/python
import sys
import inspect
import os.path
import os
def get_symbols_from_module(python_module, filter_func):
members = inspect.getmembers(python_module)
return dict([[name,symbol] for name,symbol in members
if filter_func(symbol)])
def main():
# Obtain a map whith all the classes defined in the
# standard os module
print get_symbols_from_module(os, inspect.isclass)
# Obtain a map with all the functions defined in the
# standard sys module
print get_symbols_from_module(sys, inspect.isfunction)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment