Difference between os.getenv and os.environ.get(os.getenv 和 os.environ.get 的区别)
问题描述
这两种方法有什么区别吗?
Is there any difference at all between both approaches?
>>> os.getenv('TERM')
'xterm'
>>> os.environ.get('TERM')
'xterm'
>>> os.getenv('FOOBAR', "not found") == "not found"
True
>>> os.environ.get('FOOBAR', "not found") == "not found"
True
它们似乎具有完全相同的功能.
They seem to have the exact same functionality.
推荐答案
参见 此相关线程.基本上,os.environ
是在导入时找到的,而 os.getenv
是 os.environ.get
的包装器,至少在 CPython 中是这样.
See this related thread. Basically, os.environ
is found on import, and os.getenv
is a wrapper to os.environ.get
, at least in CPython.
为了回复评论,在 CPython 中,os.getenv
基本上是 os.environ.get
的快捷方式;因为 os.environ
是在 os
的导入时加载的,只有这样,同样适用于os.getenv
.
To respond to a comment, in CPython, os.getenv
is basically a shortcut to os.environ.get
; since os.environ
is loaded at import of os
, and only then, the same holds for
os.getenv
.
这篇关于os.getenv 和 os.environ.get 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:os.getenv 和 os.environ.get 的区别


- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01