Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么是';as';接线员怎么办?_Python_Methods_Operator Keyword - Fatal编程技术网

Python 什么是';as';接线员怎么办?

Python 什么是';as';接线员怎么办?,python,methods,operator-keyword,Python,Methods,Operator Keyword,我正在从Zed Shaw的《艰苦学习Python》学习Python的基础知识。我目前在第36章(符号回顾),遇到了“as”操作员。我需要在Python中搜索它的用途。我知道这个问题涉及面很广,但我在python.docs上没有找到任何类似的内容。 这个操作符是做什么的?它与with关键字一起使用: with open("file.foo") as f: # Do something with f pass 它用于在导入模块时或使用with子句时创建别名: import pand

我正在从Zed Shaw的《艰苦学习Python》学习Python的基础知识。我目前在第36章(符号回顾),遇到了“as”操作员。我需要在Python中搜索它的用途。我知道这个问题涉及面很广,但我在python.docs上没有找到任何类似的内容。
这个操作符是做什么的?

它与
with
关键字一起使用:

with open("file.foo") as f:
    # Do something with f
    pass

它用于在导入模块时或使用
with
子句时创建别名:

import pandas as pd

df = pd.DataFrame()

#####

with open(file_name, 'r') as my_file:
    my_file.readlines()

看看吧。不知道为什么这被否决了——在我看来是合理的。向上投票以补偿。嗯,“with”语句似乎是“as”的另一个用法,所以我不理解向下投票……如果你是从在线课程或文本中学习,那么评论不应该基于材料吗?
as
关键字不在其中的某个地方吗?很清楚,它根本不是运算符。它只是一个关键字,可以在两个不同的语句中使用,
import
语句和
with
语句。