Testing if a global is defined in a module

Tim Johnson tim at johnsons-web.com
Mon Jul 4 17:59:13 EDT 2011


* rantingrick <rantingrick at gmail.com> [110704 13:47]:
> On Jul 4, 3:30 pm, Tim Johnson <t... at johnsons-web.com> wrote:
> >
> >   Thanks for the reply: *but*
> >   dir(<targetmodule>) will also show globals from other modules imported
> >   by the target module. So I would need a way to distinguish between
> >   those imported and those defined in <targetmodule>
> 
> Okay, then do some processing on the source. You can use regexps or
> the module mentioned earlier by Chris.
  I think I'm making this unnecessarily complicated. I had used
  something like:

  from spam import TestAddresses ## just for grins

  Which is something that I almost never do in practice.

  Also, you pointed out the UC naming convention, which I was
  unacquainted with, being self-employed, self-taught and a one-man
  crew who doesn't spend as much time as he should reading PEPs.

  I'm going to go for the predicate test as second argument to
  getmembers with something like:
def isdata(self,obj,name):
    """Check if an object is of a type that probably means it's data."""
    return (not (inspect.ismodule(obj) or inspect.isclass(obj) or
                inspect.isroutine(obj) or inspect.isframe(obj) or
                inspect.istraceback(obj) or inspect.iscode(obj)))
                and name.issupper()
## Untested code

thanks again
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com



More information about the Python-list mailing list