convert a string to a method call ?

Werner Schiendl ws-news at gmx.at
Thu Nov 15 11:59:53 EST 2001


Hi,

how about

>>> class X:
...  def bunch(self):
...   print "bunch"
...  def of(self):
...   print "of"
...  def methods(self):
...   print "methods"
...
>>> xObj = X()
>>> meth = "of"
>>> tocall = getattr(xObj, meth)
>>> tocall()
of
>>> meth = "bunch"
>>> tocall = getattr(xObj, meth)
>>> tocall()
bunch

hth
Werner

"Bruce Edge" <bedge at troikanetworks.com> wrote in message
news:25SI7.32$Qh1.10549 at newsfeed.slurp.net...
> I have an object, say xObj, which has a bunch of methods.
> I have the method I want to call in a string, say str.
>
> How do I call method str on xObj?
>
> Use eval? If so, the syntax eludes me.
>
> TIA, Bruce.





More information about the Python-list mailing list