为什么要使用`从模块导入a作为a的`而而不是从模块导入a'
在阅读FastApi的源代码时,这一行使我模糊:
from starlette.testclient import TestClient as TestClient
为什么不只是: from starlette.testclient Import Testclient
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从可执行代码的角度来看,两个不同的代码示例生成的python字节码(使用Python 3.9)绝对没有区别:
如图所示,它们完全相同。 (相关问题 存在 //peps.python.org/pep-0484/“ rel =“ nofollow noreferrer”> pep-0484 )。
但是, Graham501617的评论指出了现代类型的提示验证器(例如
mypy
)接受此特定语法以表示该导入名称的重新出口(另一个是____________________
,值得庆幸的是,他们最终确实得到了支持,因为这已经是如此,因为这已经是如此,因为这已经如此, a 标准语法以自Python 2 以来表示符号(re-)导出符号) 。具体而言,根据查看直接链接到文件的相关差异)表示正在解决此特定类型的提示问题(作为这个问题)以确保mypy将这些导入的名称视为re-Exexport ,从而允许使用 flag(
- 严格
可能已隐式启用)。然而,此特定的重新出口语法非常严格,对“ 的名称必须相同”,相关问题中引用的语法(即)可以在某些现代软件包的推荐中找到(例如,Pytorch建议使用
导入Torch.nn作为NN
,如这个问题),实际上不允许bar
(或nn
)符号从当前模块重新出口为foo.bar
与bar
不相同代码>(同样是torch.nn
与nn
不同,因为将整个令牌torch.nn
评估而不是仅仅是最终。
之后的最终标识符)。From the point of view of executable code, there is absolutely no difference in terms of the Python bytecode being generated by the two different code examples (using Python 3.9):
As shown, they are exactly identical. (Related question asked before existence of PEP-0484).
However, the comment by Graham501617 noted how modern type hinting validators (such as
mypy
) accept this particular syntax to denote the re-export of that imported name (the other being the__all__
, which thankfully they did end up correctly supporting as that has been a standard syntax to denote symbols to (re-)export since Python 2). Specifically, as per the description of Stub Files in the referenced PEP 0484, quote:Looking at
git blame
for the relevant file in the packages pointed to this commit (direct link to relevant diff for the file) indicated that this particular type hinting issue was being addressed (as part of this issue) to ensure mypy will treat those imported names as re-export, thus allowing the usage of the--no-implicit-reexport
flag (which--strict
has likely implicitly enabled).This particular re-export syntax however is very much strict about the "name before and after
as
must be the same", the syntax referred in the related question (i.e.import foo.bar as bar
) can be found in recommendation by certain modern packages (e.g. PyTorch recommends the use ofimport torch.nn as nn
, as discussed in this question), does not in fact allow thebar
(ornn
) symbol be re-exported from the current module asfoo.bar
is not the same asbar
(likewisetorch.nn
is not the same asnn
, as the whole set of tokenstorch.nn
is evaluated instead of the just the final identifier after the final.
).