Created
December 8, 2010 04:54
-
-
Save m8ttyB/732905 to your computer and use it in GitHub Desktop.
Python object introspection
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
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