What is the type of os.environ? and why it does not support viewkeys method

If I print os.environ , the output looks like a dictionary. Some of the posts I read on the internet say this is a memory-based dictionary. But it does not support the .viewkeys() method and tells me that: _Environ instance does not support this method . So what exactly is the type of os.environ . If I try:

 print type(os.environ) 

As an answer, I get instance .

Can you clarify this os.environ behavior?

+4
source share
4 answers
 >>> os.environ.__class__ <class os._Environ at 0xb7865e6c> 

This is a subclass of UserDict.IterableUserDict .

In python 2.7, the source can be found in os.py on line 413 (Windows) and line 466 (Posix). Here is the source of python 3.2.

+5
source

This is an instance of os._Environ :

 >>> os.environ.__class__ <class os._Environ at 0x01DDA928> 

It is defined in the Python library, the os.py file os.py and cannot be a simple dictionary, because updating the dictionary should also update the process environment. Also, key searches should be case insensitive in Windows.

In Python 2.x, it subclasses UserDict.IterableUserDict , which apparently does not have a new viewkeys() method. In Python 3.x, it implements MutableMapping abc, but does not have other explicit base classes.

+4
source

os.environ is the mapping object. dict is the type of the mapping object, and os.environ not a dict . Has the meaning?

0
source

os.environ is an instance of a class,

to try:

 os.environ.__dict__ 

he will give you all the attributes.

0
source

Source: https://habr.com/ru/post/1396158/


All Articles