0

I recently stumbled across a line of code running on Python 3.7 which I hadn't seen before and couldn't find anything online as I didn't know what to search.

The context is similar to the following:

def some_function(some_var: bool = None):

    if some_var is None:
        some_var = os.environ.get("SOME_ENV_VAR", False) == "true"

What does the trailing double equals do here and why would it be used?

1
  • It's just a comparison. some_var is set to the result of comparing the return value of get to the string "true". Commented Apr 23, 2020 at 14:37

2 Answers 2

3

You can rewrite this piece of code as the following to see more clearly what it's doing.

if some_var is None:
   if os.environ.get("SOME_ENV_VAR", False) == "true":
       some_var = True
   else
       some_var = False

This line:

os.environ.get("SOME_ENV_VAR", False) == "true"

is a conditional check and then some_var would be assigned the result of the True/False check.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no exotic syntax here. == is just a binary (as in "two arguments") operator, just like + or and.

You can see the line as a = b == c, and just like a = b + c would mean "Compute b + c and store that in a", this means "Compute b == c and store that in a, ie. put True in a if b is equal to c, False otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.