Home>

I have a question about the interpretation of the python code below.
What is the interpretation when only variables are described in the condition of the if statement?

Set the OS environment variable TEST_Prefix to the #incoming_test variable
incoming_test_ = os.environ ['TEST_Prefix']
    if incoming_test:
        object_path = (incoming_test + "/" + message_id)
    else: else:
        object_path = message_id


I interpreted it as follows,

If incoming_test is ""
Set the object_path variable to (incoming_test + "/" + message_id)
Otherwise
Set message_id in object_path variable

I don't understand the "" part.
I'm sorry for the introductory question, but please teach me.

  • Answer # 1

    8. compound statement — Python 3.9.0 documentation

    The if statement evaluates the expressions one by one, continues until true, and selects only the suite of clauses that are true (for the definitions of true: true and false: false, Boolean operations () boolean operation) See section);

    6. expression — Python 3.9.0 documentation

    In the context of Boolean operations and when expressions are used in control flow statements, the following values ​​are interpreted as false: False, None, number 0 for all types, empty string, empty container (Strings, tuples, lists, dictionaries, sets, frozen sets, etc.). Any other value is interpreted as true.

  • Answer # 2

    Truth value judgment

    The object is determined to be true by default. However, the class isbool() If you define a method and it returns False, orlenIf you have defined a () method and it returns 0, it is considered false.

    So, roughly speaking, I think it's okay if the value of os.environ ['TEST_Prefix'] is not None and its value (character string) is not empty.