【问题标题】:Does Python have a method that returns all the attributes in a module?Python 是否有返回模块中所有属性的方法?
【发布时间】:2010-09-14 21:03:06
【问题描述】:

我已经在 Google 上搜索过了,但运气不好。

【问题讨论】:

    标签: python module attributes return


    【解决方案1】:

    除了已经提到的dir 内置函数,还有inspect 模块,它有一个非常好的getmembers 方法。结合pprint.pprint,你有一个强大的组合

    from pprint import pprint
    from inspect import getmembers
    import linecache
    
    pprint(getmembers(linecache))
    

    一些示例输出:

     ('__file__', '/usr/lib/python2.6/linecache.pyc'),
     ('__name__', 'linecache'),
     ('__package__', None),
     ('cache', {}),
     ('checkcache', <function checkcache at 0xb77a7294>),
     ('clearcache', <function clearcache at 0xb77a7224>),
     ('getline', <function getline at 0xb77a71ec>),
     ('getlines', <function getlines at 0xb77a725c>),
     ('os', <module 'os' from '/usr/lib/python2.6/os.pyc'>),
     ('sys', <module 'sys' (built-in)>),
     ('updatecache', <function updatecache at 0xb77a72cc>)
    

    请注意,与dir 不同,您可以看到成员的实际值。您可以将过滤器应用于getmembers,这些过滤器类似于您可以应用于dir 的过滤器,它们可以更强大。例如,

    def get_with_attribute(mod, attribute, public=True):
        items = getmembers(mod)
        if public:
           items = filter(lambda item: item[0].startswith('_'), items)
        return [attr for attr, value in items if hasattr(value, attribute]
    

    【讨论】:

      【解决方案2】:
      import module
      dir(module)
      

      【讨论】:

        【解决方案3】:

        你正在寻找dir:

        import os
        dir(os)
        
        ??dir
        
            dir([object]) -> list of strings
        
        If called without an argument, return the names in the current scope.
        Else, return an alphabetized list of names comprising (some of) the attributes
        of the given object, and of attributes reachable from it.
        If the object supplies a method named __dir__, it will be used; otherwise
        the default dir() logic is used and returns:
          for a module object: the module's attributes.
          for a class object:  its attributes, and recursively the attributes
            of its bases.
          for any other object: its attributes, its class's attributes, and
            recursively the attributes of its class's base classes.
        

        【讨论】:

          【解决方案4】:

          正如已经正确指出的那样,dir 函数将返回一个包含给定对象中所有可用方法的列表。

          如果您在命令提示符下调用dir(),它将以启动时可用的方法进行响应。如果你打电话:

          import module
          print dir(module)
          

          它将打印一个包含模块module 中所有可用方法的列表。大多数时候你只对公共方法感兴趣(那些你应该使用的方法)——按照惯例,Python私有方法和变量以__开头,所以我要做的是:

          import module
          for method in dir(module):
           if not method.startswith('_'):
             print method
          

          这样你只打印 public 方法(可以肯定 - _ 只是一个约定,许多模块作者可能不遵守约定)

          【讨论】:

            【解决方案5】:

            dir 是你需要的:)

            【讨论】:

              猜你喜欢
              • 2020-09-05
              • 2013-05-14
              • 1970-01-01
              • 1970-01-01
              • 2022-01-20
              • 1970-01-01
              • 1970-01-01
              • 2021-08-22
              • 2014-08-03
              相关资源
              最近更新 更多