有没有办法在 Python 中访问父模块 [英] Is there a way to access parent modules in Python

查看:38
本文介绍了有没有办法在 Python 中访问父模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道是否有办法从子模块访问父模块.如果我导入子模块:

来自子流程导入类型

我有 types - 是否有一些 Python 魔法可以从 types 访问 subprocess 模块?类似于 ().__class__.__bases__[0].__subclasses__().

解决方案

如果您访问过一个模块,您通常可以从 sys.modules 字典中找到它.Python 不会保留带有名称的父指针",特别是因为这种关系不是一对一的.例如,使用您的示例:

<预><代码>>>>从子流程导入类型>>>类型<来自'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc'的模块'types'>>>>导入系统>>>sys.modules['子进程']<来自'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc'的模块'subprocess'>

如果您注意到 subprocess 模块中存在的 types 只是其中的 import types 语句的产物.如果您需要该模块,您只需导入类型.

事实上,subprocess 的未来版本可能不会再导入 types,你的代码就会崩溃.您应该只导入出现在模块的 __all__ 列表中的名称;考虑其他名称作为实现细节.

例如:

<预><代码>>>>导入子流程>>>目录(子进程)['CalledProcessError', 'MAXFD', 'PIPE', 'Popen', 'STDOUT', '_PIPE_BUF', '__all__', '__builtins__', '__doc__','__file__', '__name__', '__package__', '_active', '_cleanup', '_demo_posix', '_demo_windows', '_eintr_retry_call','_has_poll', 'call', 'check_call', 'check_output', 'errno', 'fcntl', 'gc', 'list2cmdline', 'mswindows', 'os','pickle', 'select', 'signal', 'sys', 'traceback', 'types']>>>子进程.__all__['Popen', 'PIPE', 'STDOUT', 'call', 'check_call', 'check_output', 'CalledProcessError']

您可以看到 subprocess 中可见的大多数名称只是它导入的其他顶级模块.

I need to know if there is a way to access parent modules from submodules. If I import submodule:

from subprocess import types

I have types - is there some Python magic to get access to subprocess module from types? Something similar to this for classes ().__class__.__bases__[0].__subclasses__().

解决方案

If you've accessed a module you can typically get to it from the sys.modules dictionary. Python doesn't keep "parent pointers" with names, particularly because the relationship is not one-to-one. For example, using your example:

>>> from subprocess import types
>>> types
<module 'types' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc'>
>>> import sys
>>> sys.modules['subprocess']
<module 'subprocess' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc'>

If you'll note the presence of types in the subprocess module is just an artifact of the import types statement in it. You just import types if you need that module.

In fact, a future version of subprocess may not import types any more, and your code will break. You should only import the names that appear in the __all__ list of a module; consider other names as implementation details.

So, for example:

>>> import subprocess
>>> dir(subprocess)
['CalledProcessError', 'MAXFD', 'PIPE', 'Popen', 'STDOUT', '_PIPE_BUF', '__all__', '__builtins__', '__doc__',
 '__file__', '__name__', '__package__', '_active', '_cleanup', '_demo_posix', '_demo_windows', '_eintr_retry_call',
 '_has_poll', 'call', 'check_call', 'check_output', 'errno', 'fcntl', 'gc', 'list2cmdline', 'mswindows', 'os',
 'pickle', 'select', 'signal', 'sys', 'traceback', 'types']
>>> subprocess.__all__
['Popen', 'PIPE', 'STDOUT', 'call', 'check_call', 'check_output', 'CalledProcessError']

You can see that most of the names visible in subprocess are just other top-level modules that it imports.

这篇关于有没有办法在 Python 中访问父模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆