Skip to content

Instantly share code, notes, and snippets.

@m8ttyB
Created December 8, 2010 04:54
Show Gist options
  • Save m8ttyB/732905 to your computer and use it in GitHub Desktop.
Save m8ttyB/732905 to your computer and use it in GitHub Desktop.
Python object introspection
class InspectorGadget(object):
def __init__(self, quarrel_with):
self.__evil_antagonist = quarrel_with
def go_go_gadget_hat(self):
... deploy hat
def go_go_gadget_arms(self):
... do that crazy arm thing
def meet_penny_and_brain_for_icecream(self):
... magic happens here
if __name__ == '__main__':
# a list for inspector objects ready to save the world
inspector_list = list()
inspector_list.append( InspectorGadget("Dr. Claw") )
inspector_list.append( InspectorGadget("The Cuckoo Clockmaker") )
for inspector in inspector_list:
# get a list of the object's public methods
methods = inspector.__class__.__dic__.keys()
# iterate over the method list & call those that start with 'go_go_gadget'
for method in methods:
if method.startswith('go_go_gadget'):
getattr(obj, method)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment