What's the pythonic way of conditional variable initialization?

前端 未结 5 1033
情书的邮戳
情书的邮戳 2021-02-07 16:56

Due to the scoping rules of Python, all variables once initialized within a scope are available thereafter. Since conditionals do not introduce new scope, constructs in other l

5条回答
  •  难免孤独
    2021-02-07 17:05

    The answer depends on if there are side effects of get_message() which are wanted.

    In most cases clearly the second one wins, because the code which produces the unwanted result is not executed. But if you need the side effects, you should choose the first version.

提交回复
热议问题