has_method (solution found)

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Tue Aug 31 17:29:34 CEST 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