I want to check my environment for the existence of a variable, say
if "FOO" in os.environ:
if os.getenv("FOO") is not None:
Use the first; it directly tries to check if something is defined in
environ. The second form works equally well but lacks sense since you actually get a value back if it exists and don't use it.
You're trying to see if something is present in
environ, why would you get just to compare it and then toss it away?
That's exactly what
Get an environment variable, return
Noneif it doesn't exist. The optional second argument can specify an alternate default.
(this also means your check could just be
you don't want to get it, you want to check for it's existence. Either way,
getenv is just a wrapper around
environ.get but you don't see people checking for membership in mappings with:
if environ.get('Foo') is not None: