john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

getattr object method from string

class Printer( object ):

  def foo( self ):
    print "hi"


class MyExec( object ):

  def myrun( self, object_instance, method_name ):
    print type( object_instance )
    if hasattr( object_instance, method_name ):
      print "has", method_name
      method_to_call = getattr( object_instance, method_name )
      print type( method_to_call )
      method_to_call()



if __name__ == "__main__":
  p = Printer()
  p.foo()

  me = MyExec()
  me.myrun( p, "foo" )

  • « Networking 802.1x command line ubuntu 13.04 self signed ca INCOMPLETE
  • Mysql non interactive truncate variables »

Published

Aug 24, 2013

Category

python

~45 words

Tags

  • from 24
  • getattr 1
  • method 3
  • object 7
  • python 180
  • string 18