Checking for attribute in the current module with getattr( ) 
Author Message
 Checking for attribute in the current module with getattr( )

Hello,

I have written a quite small and simple python script, schematically
looking like this:

    #!/usr/local/bin/python

    def func1(arg1,arg2):
        ....

    def func2(arg1,arg2):
        ....

    ## Start of main program

    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?

Best regards

Joakim Hove

--
==== Joakim Hove      www.phys.ntnu.no/~hove/   =======================
|| Institutt for fysikk  (735) 93637 / E3-166  |  Sk?yensgate 10D    ||

================================================= 73 93 31 68 =========



Sun, 25 Jan 2004 03:04:22 GMT  
 Checking for attribute in the current module with getattr( )

Quote:
> 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"

Hope this helps,

Lyle



Sun, 25 Jan 2004 03:20:29 GMT  
 Checking for attribute in the current module with getattr( )

Quote:

> > 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



Sun, 25 Jan 2004 03:44:29 GMT  
 Checking for attribute in the current module with getattr( )

    ...

Quote:
>     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



Sun, 25 Jan 2004 03:41:51 GMT  
 Checking for attribute in the current module with getattr( )
Hi Lyle, Just and Alex,

thanks for the suggestions, it works like a champ now.

Best regards - Joakim

--
==== Joakim Hove      www.phys.ntnu.no/~hove/   =======================
|| Institutt for fysikk  (735) 93637 / E3-166  |  Sk?yensgate 10D    ||

================================================= 73 93 31 68 =========



Sun, 25 Jan 2004 14:58:16 GMT  
 Checking for attribute in the current module with getattr( )


Quote:

> > > 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"

That's slightly wider than the original poster had asked for
(and Lyle provided), although it may of course be what the
original poster *wanted* (i.e., it's possible that the o.p.
didn't express himself entirely clearly).  Just's version
says "this module defines func1" even if func1 was really
installed into __builtins__ by some OTHER module, for ex...

Alex



Sun, 25 Jan 2004 17:24:55 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. static attributes & __getattr__ override

2. argument checking before getattr

3. ANN:modeler __getattr__ for modules

4. Is there a module __getattr__ function?

5. modeler: __getattr__ for modules

6. Modules with __getattr__ (Re: PEP 317: Eliminate Implicit Exc eption Instantiation)

7. __getattr__ functionality for modules?

8. "like Current" and static type checking

9. Constraint checking of actuals passed to Attributes

10. Constraint checking in attributes

11. Any command to check current resoruce usage?

12. Checking file attributes

 

 
Powered by phpBB® Forum Software