You can often learn what Python code is doing by looking at its pieces in the Python interpreter:
>>> vars_of_interest
('AA_PYTHON', 'GG_POST_DEV_ELMO', 'GG_DEBUG_ELMO')
>>> import os
>>> [(var, os.getenv(var, 0)) for var in vars_of_interest]
[('AA_PYTHON', 0), ('GG_POST_DEV_ELMO', 0), ('GG_DEBUG_ELMO', 0)]
>>> env = dict((var, os.getenv(var, 0)) for var in vars_of_interest)
>>> env
{'AA_PYTHON': 0, 'GG_DEBUG_ELMO': 0, 'GG_POST_DEV_ELMO': 0}
env = dict(...)
makes env
a dict
. If you are ever unsure what an object is, you can always ask it what its type is:
>>> type(env)
dict
A dict
is a mapping between keys and values.
In this case, env
is a mapping between strings such as 'AA_PYTHON'
and values, such as 0
.
var
is a temporary variable used in the generator expression
((var, os.getenv(var, 0)) for var in vars_of_interest)
The for var in vars_of_interest
in the generator expression tells Python to iterate over the items in the tuple vars_of_interest
, and assign the values to var
one-by-one as it iterates through the loop.
The generator expression is an iterator. The iterator yields the values of (var, os.getenv(var, 0))
for each var
.
The expression (var, os.getenv(var, 0))
is a tuple which can be thought of as a key-value pair. var
is the key, os.getenv(var, 0)
is the value. os.getenv looks up the environment variable var
(e.g. 'AA_PYTHON'
) and returns the value of the environment variable if it exists, otherwise, it returns 0.
When dict
is passed an iterator of key-value pairs, as is being done in the expression
dict((var, os.getenv(var, 0)) for var in vars_of_interest)
it returns a dict
which maps the given keys to the given values.
See here for more information on Python dicts.