has_method

Andrew Durdin adurdin at gmail.com
Tue Aug 31 21:47:14 EDT 2004


From: Gandalf <gandalf at geochemsource.com>
You are right - I only used dir() interactively. I have the answer,
how to check.
 But how can I access that method? I would like to call it by name. :-)
 
Use the getattr() builtin:


class Foo:
    def mymeth(self):
            print "This is mymeth happening"

methname = 'mymeth'
foo = Foo()
try:
    meth = getattr(foo, methname)
except AttributeError:
    print "Method", methname, "does not exist."
else:
    meth()



More information about the Python-list mailing list