python变量声明语句_python – 在条件语句中声明变量有问题吗?
在條件的所有可能分支中重新定義變量之前,它是否可以防止定義變量?
比如應該這個代碼:
# Condition could fail
try:
textureIndices = someExpression()
# textureIndices is defined here if it does
except:
textureIndices = []
return textureIndices
重寫為:
# textureIndices is defined early and then re-defined in the conditional
textureIndices = None
try:
textureIndices = someExpression()
except:
textureIndices = 66
return textureIndices
或者,因為除了打開其他問題,這里的textureIndices的定義是否存在問題:
if condition:
textureIndices = someExpression()
else:
textureIndices = 66
return textureIndices
減少問題?
唯一的區別是在第二個版本中textureIndices是在條件之外定義的.
我不明白為什么它很重要因為textureIndices不可能在條件中沒有賦值,但我可以看到為什么從管家的角度來看,知道變量被分配給某些東西是件好事.
例如,如果第一個示例中沒有except語句,則不會始終定義textureIndices并且返回會導致錯誤.
但是,如果沒有轉發定義在條件的兩個原因中定義的變量,是否存在問題?
總結
以上是生活随笔為你收集整理的python变量声明语句_python – 在条件语句中声明变量有问题吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: labelme 语义分割数据集_Rang
- 下一篇: websocket python爬虫_p