Confused about hasattr/getattr/namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Roberts

    Confused about hasattr/getattr/namespaces

    I'm confused about the use of hasattr/getattr, or possibly
    namespaces. I know how to do this:

    class UnderstandThis( object):
    def do_foo(self): pass
    def do_bar(self): pass
    def doit(self, cmd):
    funcname = 'do_' + cmd
    if hasattr(self, funcname):
    getattr(self, funcname)()
    else:
    print 'not found'

    But if I try to do this with classes in a module (instead of
    functions in a class):

    class FooGenerator(ob ject): ...
    class BarGenerator(ob ject): ...

    def generate(cmd):
    clsname = cmd + 'Generator'
    if hasattr(???, clsname):
    etc...

    I don't know what to use for ??? instead of self. If I print
    globals() inside generate() it shows the two classes, but using
    it in the hasattr line doesn't work -- huh? I can make it work
    by wrapping the generate function in a (otherwise useless) class
    and using self -- again, huh?

    Couldn't find an explanation or example in the docs. In fact,
    the description of vars/globals/locals (in section 2.1) just
    confused me even more. Think I'm heading down the wrong path.
    Can somebody please un-confuse me?

    Brian.
  • Bob Ippolito

    #2
    Re: Confused about hasattr/getattr/namespaces

    On 2004-02-29 13:48:21 -0500, brian@mirror.or g (Brian Roberts) said:
    [color=blue]
    > I'm confused about the use of hasattr/getattr, or possibly
    > namespaces. I know how to do this:
    >
    > class UnderstandThis( object):
    > def do_foo(self): pass
    > def do_bar(self): pass
    > def doit(self, cmd):
    > funcname = 'do_' + cmd
    > if hasattr(self, funcname):
    > getattr(self, funcname)()
    > else:
    > print 'not found'
    >
    > But if I try to do this with classes in a module (instead of
    > functions in a class):
    >
    > class FooGenerator(ob ject): ...
    > class BarGenerator(ob ject): ...
    >
    > def generate(cmd):
    > clsname = cmd + 'Generator'
    > if hasattr(???, clsname):
    > etc...
    >
    > I don't know what to use for ??? instead of self. If I print
    > globals() inside generate() it shows the two classes, but using
    > it in the hasattr line doesn't work -- huh? I can make it work
    > by wrapping the generate function in a (otherwise useless) class
    > and using self -- again, huh?
    >
    > Couldn't find an explanation or example in the docs. In fact,
    > the description of vars/globals/locals (in section 2.1) just
    > confused me even more. Think I'm heading down the wrong path.
    > Can somebody please un-confuse me?[/color]

    if you have the module object, you can use that.. or you could use
    sys.modules[__name__] to get the current module (import sys, of course).

    globals(), locals() are dictionaries, so you would have to do "clsobj =
    globals().get(c lsname)" or something like that. You *probably* want
    something more explicit than that though.. it probably wouldn't be too
    much work to keep your own command->class dictionary.

    -bob

    Comment

    • Jeff Epler

      #3
      Re: Confused about hasattr/getattr/namespaces

      Here's one way:
      def generate(cmd):
      clsname = cmd + "Generator"
      g = globals()
      if g.has_key(clsna me): ...
      globals() returns the module's dict, not the module. You could also
      import modulename as self
      but this is a kind of circular import, and the re-use of the name "self"
      (which will be used in method definitions) smells bad too.

      Jeff

      Comment

      Working...