使用os.environ.setdefault是否安全? [英] Is it safe to use os.environ.setdefault?

查看:558
本文介绍了使用os.environ.setdefault是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的ipython shell中,我在os.environ中看到了一个setdefault方法,但该方法没有记录. http://docs.python.org/library/os.html#os.environ .在其他地方有记录吗?

From my ipython shell, I see a method setdefault in os.environ but it is not documented. http://docs.python.org/library/os.html#os.environ. Is it documented somewhere else?

def setdefault(self, key, failobj=None):
    if key not in self:
        self[key] = failobj
    return self[key]

我可以使用此功能或为这些行写一个包装器吗?

Can I use this function or write a wrapper for those lines?

推荐答案

os.environ文档确实声明它是一个映射:

The os.environ documentation does state it's a mapping:

表示字符串环境的映射对象.

A mapping object representing the string environment.

因此,它的行为符合python 映射文档,其中dict是标准实现.

As such it behaves according to the python mapping documentation of which dict is the standard implementation.

os.environ的行为与标准dict一样,它具有所有相同的方法:

os.environ therefor behaves just like the standard dict, it has all the same methods:

>>> import os
>>> len(os.environ)
36
>>> 'USER' in os.environ
True
>>> os.environ.fromkeys
<bound method classobj.fromkeys of <class os._Environ at 0x107096ce8>>

.setdefault方法记录在与映射其余部分相同的页面上方法,您可以按原样使用它.

The .setdefault method is documented on the same page as the rest of the mapping methods, and you can use it just fine as is.

这篇关于使用os.environ.setdefault是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆