samedi 19 novembre 2016

What is a good practice to check if an environmental variable exists or not in Python?

I want to check my environment for the existence of a variable, say "FOO", in Python.
For this purpose, I am using the os standard library.
After reading the library's documentation, I have figured out 2 ways to achieve my goal:

Method 01:

if "FOO" in os.environ: pass

Method 02:

if os.getenv("FOO") is not None: pass

I would like to know which method, if either, is a good/preferred conditional and why.

Aucun commentaire:

Enregistrer un commentaire