在as之后用逗号导入

cor*_*vid 3 python syntax import

我正在看一个回购,并遇到了一个有点奇怪的线

from flask.ext.testing import TestCase as Base, Twill
Run Code Online (Sandbox Code Playgroud)

这样导入是什么意思?我以前没见过它,不幸的是谷歌很难.

iCo*_*dez 6

该行告诉Python导入TestCaseTwill从包flask.ext.testing中导入,但要TestCase以名称导入Base.

来自文档:

如果后跟模块名称as,则以下名称as将直接绑定到导入的模块.

下面是与示范search,并match从功能re模块:

>>> from re import search as other, match
>>> match  # The name match refers to re.match
<function match at 0x02039780>
>>> other  # The name other refers to re.search
<function search at 0x02048A98>
>>> search  # The name search is not defined because it was imported as other
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'search' is not defined
>>>
Run Code Online (Sandbox Code Playgroud)