Paz*_*r80 1 python command python-3.x
我已经看过很多次但是从来没有理解asPython 3.x中的命令.你能用简单的英语解释一下吗?
它本身不是命令,它是用作with语句一部分的关键字:
with open("myfile.txt") as f:
text = f.read()
Run Code Online (Sandbox Code Playgroud)
之后的对象 as被赋予由with上下文管理器处理的表达式的结果.
另一个用途是重命名导入的模块:
import numpy as np
Run Code Online (Sandbox Code Playgroud)
所以你可以使用这个名称,np而不是numpy从现在开始.
第三个用途是让您访问Exception对象:
try:
f = open("foo")
except IOError as exc:
# Now you can access the Exception for more detailed analysis
Run Code Online (Sandbox Code Playgroud)
它是在多种情况下用于对象命名的关键字。
from some_module import something as some_alias
# `some_alias` is `some_module.something`
with open("filename") as f:
# `f` is the file object `open("filename")` returned
try:
Nonsense!
except Exception as e:
# `e` is the Exception thrown
Run Code Online (Sandbox Code Playgroud)