Is it safe to use os.environ.setdefault?

前端 未结 2 1890
不知归路
不知归路 2021-02-05 05:12

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?

2条回答
  •  星月不相逢
    2021-02-05 06:06

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

    A mapping object representing the string environment.

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

    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
    >
    

    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.

提交回复
热议问题