What's the pythonic way of conditional variable initialization?

前端 未结 5 1775
名媛妹妹
名媛妹妹 2021-02-07 16:51

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:18

    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.

提交回复
热议问题