Python 如何检索其他模块的所有局部变量?

Python 如何检索其他模块的所有局部变量?,python,Python,我收到一个模块作为参数,我想检索它的所有局部变量(与XXX或函数或类无关)。 如何做到这一点 我试过: def _get_settings(self, module): return [setting for setting in dir(module) if not inspect.ismodule(setting) and not inspect.isbuiltin(setting) and not inspect.isfunction(setting) and not se

我收到一个模块作为参数,我想检索它的所有局部变量(与XXX或函数或类无关)。
如何做到这一点

我试过:

def _get_settings(self, module):
        return [setting for setting in dir(module) if not inspect.ismodule(setting) and not inspect.isbuiltin(setting) and not inspect.isfunction(setting) and not setting.__NAME__.startswith('__')]
但它提出:

Traceback (most recent call last):
  File "/home/omer/Aptana Studio 3/plugins/org.python.pydev.debug_1.6.5.2011012519/pysrc/pydevd.py", line 1133, in <module>
    debugger.run(setup['file'], None, None)
  File "/home/omer/Aptana Studio 3/plugins/org.python.pydev.debug_1.6.5.2011012519/pysrc/pydevd.py", line 918, in run
    execfile(file, globals, locals) #execute the script
  File "/root/Aptana Studio 3 Workspace/website/website/manage.py", line 11, in <module>
    import settings
  File "/root/Aptana Studio 3 Workspace/website/website/settings.py", line 7, in <module>
    settings_loader = Loader(localsettings)
  File "/root/Aptana Studio 3 Workspace/website/website/envconf/loader.py", line 6, in __init__
    self.load(environment)
  File "/root/Aptana Studio 3 Workspace/website/website/envconf/loader.py", line 9, in load
    for setting in self._get_settings(module):
  File "/root/Aptana Studio 3 Workspace/website/website/envconf/loader.py", line 16, in _get_settings
    return [setting for setting in dir(module) if not inspect.ismodule(setting) and not inspect.isbuiltin(setting) and not inspect.isfunction(setting) and not setting.__NAME__.startswith('__')]
AttributeError: 'str' object has no attribute '__NAME__'
回溯(最近一次呼叫最后一次):
文件“/home/omer/aptanastudio3/plugins/org.python.pydev.debug_1.6.5.2011012519/pysrc/pydevd.py”,第1133行,在
运行(安装程序['file'],无,无)
文件“/home/omer/aptanastudio3/plugins/org.python.pydev.debug_1.6.5.2011012519/pysrc/pydevd.py”,第918行,正在运行
execfile(文件、全局、局部)#执行脚本
文件“/root/aptanastudio3 Workspace/website/website/manage.py”,第11行,在
导入设置
文件“/root/aptanastudio3 Workspace/website/website/settings.py”,第7行,在
设置\u加载程序=加载程序(本地设置)
文件“/root/aptanastudio3 Workspace/website/website/envconf/loader.py”,第6行,在__
自加载(环境)
文件“/root/aptanastudio3 Workspace/website/website/envconf/loader.py”,第9行,加载
用于在self中设置。获取设置(模块):
文件“/root/aptanastudio3 Workspace/website/website/envconf/loader.py”,第16行,在“获取”设置中
返回[dir(模块)中设置的设置(如果不检查).ismodule(设置)和not inspect.isbuiltin(设置)和not inspect.isfunction(设置)和not setting.\uuuu名称\uuuuuu.startswith(“”\uuuuu')]
AttributeError:'str'对象没有属性'\u\u NAME\u'

dir()
返回字符串列表。直接使用
setting.startswith()

dir()
返回字符串列表。直接使用
setting.startswith()

您可以使用
dir()
访问所有局部变量。这将返回一个字符串列表,其中每个字符串都是属性的名称。这将返回所有变量和方法。如果只查找实例变量,可以通过
\uuuuu dict\uuuu
访问这些变量,例如:

>>> class Foo(object):
...     def __init__(self, a, b, c):
>>>
>>> f = Foo(1,2,3)
>>> f.__dict__
{'a': 1, 'c': 3, 'b': 2}
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c']

您可以使用
dir()
访问所有局部变量。这将返回一个字符串列表,其中每个字符串都是属性的名称。这将返回所有变量和方法。如果只查找实例变量,可以通过
\uuuuu dict\uuuu
访问这些变量,例如:

>>> class Foo(object):
...     def __init__(self, a, b, c):
>>>
>>> f = Foo(1,2,3)
>>> f.__dict__
{'a': 1, 'c': 3, 'b': 2}
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c']

你的意思可能是
\uuuuu name\uuuuu
,而不是
\uuuu name\uuuuu
。Python是区分大小写的。您可能指的是
\uuuuuu name\uuuuuuu
,而不是
\uuuuuuu name\uuuuuuu
。Python区分大小写。