为什么 Python 的 __import__ 需要 fromlist? [英] Why does Python's __import__ require fromlist?

查看:58
本文介绍了为什么 Python 的 __import__ 需要 fromlist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,如果你想以编程方式导入一个模块,你可以这样做:

In Python, if you want to programmatically import a module, you can do:

module = __import__('module_name')

如果你想导入一个子模块,你会认为这很简单:

If you want to import a submodule, you would think it would be a simple matter of:

module = __import__('module_name.submodule')

当然,这是行不通的;您只需再次获得 module_name.你必须这样做:

Of course, this doesn't work; you just get module_name again. You have to do:

module = __import__('module_name.submodule', fromlist=['blah'])

为什么? fromlist 的实际值似乎根本无关紧要,只要它不为空即可.要求一个参数,然后忽略它的值有什么意义?

Why? The actual value of fromlist don't seem to matter at all, as long as it's non-empty. What is the point of requiring an argument, then ignoring its values?

Python 中的大多数东西似乎都有充分的理由,但对于我的一生,我无法为这种行为的存在提出任何合理的解释.

Most stuff in Python seems to be done for good reason, but for the life of me, I can't come up with any reasonable explanation for this behavior to exist.

推荐答案

其实__import__()的行为完全是因为import语句的实现,它调用 __import__().__import__() 可以通过 import 调用基本上有五种略有不同的方式(有两个主要类别):

In fact, the behaviour of __import__() is entirely because of the implementation of the import statement, which calls __import__(). There's basically five slightly different ways __import__() can be called by import (with two main categories):

import pkg
import pkg.mod
from pkg import mod, mod2
from pkg.mod import func, func2
from pkg.mod import submod

在第一个 第二种情况下,import 语句应该将最左边"的模块对象分配给最左边"的名称:.import pkg.mod 之后你可以做 pkg.mod.func() 因为import 语句引入了本地名称 pkg,这是一个具有 mod 属性的模块对象.因此,__import__() 函数必须返回最左边"的模块对象,以便将其分配给 pkg.这两个导入语句因此转化为:

In the first and the second case, the import statement should assign the "left-most" module object to the "left-most" name: pkg. After import pkg.mod you can do pkg.mod.func() because the import statement introduced the local name pkg, which is a module object that has a mod attribute. So, the __import__() function has to return the "left-most" module object so it can be assigned to pkg. Those two import statements thus translate into:

pkg = __import__('pkg')
pkg = __import__('pkg.mod')

在第三、第四和第五种情况下,import 语句必须做更多的工作:它必须分配给(可能)多个名称,它必须从模块对象中获取.__import__() 函数只能返回一个对象,并且没有真正的理由让它从模块对象中检索每个名称(这会使实现变得更加复杂.)所以简单的方法类似于(对于第三种情况):

In the third, fourth and fifth case, the import statement has to do more work: it has to assign to (potentially) multiple names, which it has to get from the module object. The __import__() function can only return one object, and there's no real reason to make it retrieve each of those names from the module object (and it would make the implementation a lot more complicated.) So the simple approach would be something like (for the third case):

tmp = __import__('pkg')
mod = tmp.mod
mod2 = tmp.mod2

但是,如果 pkg 是一个包并且 modmod2 是该包中的模块 ,那么这将不起作用尚未导入,因为它们在第三种和第五种情况下.__import__() 函数需要知道 modmod2import 语句想要的名称可访问,以便它可以查看它们是否是模块并尝试导入它们.所以调用更接近于:

However, that won't work if pkg is a package and mod or mod2 are modules in that package that are not already imported, as they are in the third and fifth case. The __import__() function needs to know that mod and mod2 are names that the import statement will want to have accessible, so that it can see if they are modules and try to import them too. So the call is closer to:

tmp = __import__('pkg', fromlist=['mod', 'mod2'])
mod = tmp.mod
mod2 = tmp.mod2

导致 __import__() 尝试加载 pkg.modpkg.mod2 以及 pkg> (但如果 modmod2 不存在,则它不是 __import__() 调用中的错误;产生错误留给import 语句.)但是对于第四个和第五个示例,这仍然不是正确的事情,因为如果调用是这样的:

which causes __import__() to try and load pkg.mod and pkg.mod2 as well as pkg (but if mod or mod2 don't exist, it's not an error in the __import__() call; producing an error is left to the import statement.) But that still isn't the right thing for the fourth and fifth example, because if the call were so:

tmp = __import__('pkg.mod', fromlist=['submod'])
submod = tmp.submod

然后 tmp 最终会像以前一样成为 pkg,而不是您想要获得 pkg.mod 模块submod 属性来自.实现可能已经决定这样做,以便 import 语句做额外的工作,在 . 上拆分包名称,就像 __import__() 函数一样确实并遍历名称,但这意味着重复一些工作.因此,相反,实现使 __import__() 返回 最右侧 模块而不是 最左侧 一个 如果且仅如果 fromlist 被传递并且不为空.

then tmp would end up being pkg, as before, and not the pkg.mod module you want to get the submod attribute from. The implementation could have decided to make it so the import statement does extra work, splitting the package name on . like the __import__() function already does and traversing the names, but this would have meant duplicating some of the effort. So, instead, the implementation made __import__() return the right-most module instead of the left-most one if and only if fromlist is passed and not empty.

(import pkg as pfrom pkg import mod as m 语法不会改变这个故事的任何内容,除了本地名称被分配给——__import__() 函数在使用 as 时没有什么不同,它都保留在 import 语句实现中.)

(The import pkg as p and from pkg import mod as m syntax doesn't change anything about this story except which local names get assigned to -- the __import__() function sees nothing different when as is used, it all remains in the import statement implementation.)

这篇关于为什么 Python 的 __import__ 需要 fromlist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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