getting dir(x), but not as list of strings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mh@pixar.com

    getting dir(x), but not as list of strings?

    I want to iterate over members of a module, something like:

    for i in dir(x):
    if type(i) == types.FunctionT ype: ...

    but of course dir() returns a list of strings. If x is a module,
    how can I get the list of its members as their actual types?

    Many TIA!
    Mark

    --
    Mark Harrison
    Pixar Animation Studios
  • Marc 'BlackJack' Rintsch

    #2
    Re: getting dir(x), but not as list of strings?

    On Wed, 21 May 2008 07:54:32 +0000, mh wrote:
    I want to iterate over members of a module, something like:
    >
    for i in dir(x):
    if type(i) == types.FunctionT ype: ...
    >
    but of course dir() returns a list of strings. If x is a module,
    how can I get the list of its members as their actual types?
    Take a look at the `inspect` module.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • akeppke@web.de

      #3
      Re: getting dir(x), but not as list of strings?

      Use

      for i in dir(x):
      print i # name of member as string
      print getattr(x, i) # member

      regards

      Arno


      On 21 Mai, 09:54, m...@pixar.com wrote:
      I want to iterate over members of a module, something like:
      >
      for i in dir(x):
      if type(i) == types.FunctionT ype: ...
      >
      but of course dir() returns a list of strings. If x is a module,
      how can I get the list of its members as their actual types?
      >
      Many TIA!
      Mark

      Comment

      • Gary Herron

        #4
        Re: getting dir(x), but not as list of strings?

        mh@pixar.com wrote:
        I want to iterate over members of a module, something like:
        >
        for i in dir(x):
        if type(i) == types.FunctionT ype: ...
        >
        but of course dir() returns a list of strings. If x is a module,
        how can I get the list of its members as their actual types?
        >
        Many TIA!
        Mark
        >
        >
        Use the builtin vars to get a dictionary of names and associated objects.

        import sys
        for name,ob in vars(sys).items ():
        print name,type(ob)

        Gary Herron



        setrecursionlim it <type 'builtin_functi on_or_method'>
        getfilesystemen coding <type 'builtin_functi on_or_method'>
        path_importer_c ache <type 'dict'>
        stdout <type 'file'>
        version_info <type 'tuple'>
        exc_clear <type 'builtin_functi on_or_method'>
        prefix <type 'str'>
        getrefcount <type 'builtin_functi on_or_method'>
        byteorder <type 'str'>
        ....

        Comment

        • Terry Reedy

          #5
          Re: getting dir(x), but not as list of strings?


          "Gary Herron" <gherron@island training.comwro te in message
          news:48344323.1 040401@islandtr aining.com...
          | mh@pixar.com wrote:
          | I want to iterate over members of a module, something like:
          | >
          | for i in dir(x):
          | if type(i) == types.FunctionT ype: ...
          | >
          | but of course dir() returns a list of strings. If x is a module,
          | how can I get the list of its members as their actual types?
          | >
          | Many TIA!
          | Mark
          | >
          | >
          | Use the builtin vars to get a dictionary of names and associated objects.
          |
          | import sys
          | for name,ob in vars(sys).items ():
          | print name,type(ob)

          This also works on classes, but apparently not on most other objects.



          Comment

          Working...