Checking for attribute in the current module with getattr( )

Alex Martelli aleaxit at yahoo.com
Tue Aug 7 15:41:51 EDT 2001


"Joakim Hove" <hove at phys.ntnu.no> wrote in message
news:k0nvgjzhetl.fsf at metropolis.phys.ntnu.no...
    ...
>     if hasattr(???,"func1"):   # <- Problematic line!
>         print "This module defines a function \"func1\""
>     else:
>         print "No function \"func1\" defined"
>
> 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?

import sys
thismodule = sys.modules[__name__]
if hasattr(thismodule, 'func1'):
    # and so on


Alex






More information about the Python-list mailing list