我已经看过很多次了,但一直不明白 as 命令在 Python 3.x 中的作用。你能用简单的英语解释一下吗?
请您参考如下方法:
它本身不是命令,它是用作 with statement 的一部分的关键字:
with open("myfile.txt") as f:
text = f.read()
as 之后的对象被分配由 with 上下文管理器处理的表达式的结果。
另一个用途是重命名导入的模块:
import numpy as np
因此从现在开始您可以使用名称 np 而不是 numpy。
第三个用途是让你访问一个Exception对象:
try:
f = open("foo")
except IOError as exc:
# Now you can access the Exception for more detailed analysis
