Checking for attribute in the current module with getattr( )

Just van Rossum just at letterror.com
Tue Aug 7 15:44:29 EDT 2001


Lyle Johnson wrote:

> > Now, as indicated I don't really know what object to send in to the
> > hasattr function, indicated with "???" - is what I'm trying to do
> > possible, and how should I in case do it?
> 
> How about:
> 
>     if globals().has_key("func1"):
>         print "This module defines func1"
>     else:
>         print "This one doesn't"

Or

    try:
        func1
    except NameError:
        print "This one doesn't define func1"
    else:
        print "This module defines func1"

Just



More information about the Python-list mailing list