has_method (solution found)

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Tue Aug 31 11:29:34 EDT 2004


Eric Brunel wrote:

> def getmethod(obj, name):
>    meth = getattr(obj, name)

Because he doesn't know if the method exists?  He used has_key(), after
all.

Gandalf, you can use hasattr(obj, name) first, or you can use
getattr(obj, name, None) (or some other default value than None).

-- 
Hallvard



More information about the Python-list mailing list