Secret Mustache

Adventures of a Scruffy Nerf Herder

Object reflection

22 March 2010

Last week I was working on understanding a problem and spent 30 minutes exploring reflection. My intention was to populate an array of objects and using reflection dynamically find and call the methods that interested me. After digging around Python's internals for a bit I came up with this.

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 hereif __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)()

Ultimately I discarded this solution in favor of one that fits the domain of my problem better but it was definitely an interesting digression into possibilities.

references: wikipedia on reflection

This entry was tagged as python

blog comments powered by Disqus