What's the pythonic way of conditional variable initialization?

前端 未结 5 1027
情书的邮戳
情书的邮戳 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:10

    It might be better (read: safer) to initialize your variable outside the conditions. If you have to define other conditions or even remove some, the user of message later on might get an uninitialized variable exception.

提交回复
热议问题