Hacker News new | ask | show | jobs
by saco 2240 days ago
I don't get how this can be used anywhere except when you have 2 deployment targets which seems really limited.

Also checking for environments is done weirdly, why not use

if 'ENV' not in os.environ:

instead of

ENV = os.environ.get("ENV", None)

if ENV is None:

4 comments

The practice is pretty common and a good reflex to have. In your version there would be a risk of a race condition if it was possible for the environment to change between the calls.

Now, it’s not really an issue here with environment variables. But in general, doing assignment and then walking through the cases is safer.

Maybe just a habit, that way you don't have to repeat the literal "ENV" twice. In this case it doesn't matter much though.

Also it's a bit better from the performance perspective (but not that it also matters in this case).

>(but not that it also matters in this case)

Then why mention it?

> I don't get how this can be used anywhere except when you have 2 deployment targets which seems really limited.

Why do you say that? It looks like it can handle any number of environments to me.

I actually always use the second one but with just `if ENV`, to catch the case where the variable is set but empty, which happens a lot in practice with Docker&co.