try and if - 赋值前引用的局部变量



我尝试运行此代码,但出现错误:

赋值前引用的 A4 局部变量

a1 = '/fr/store/a'
try:
a2 = '/fr/store/b/collection/3432'
a2_store = "store"
a3 = a2.split ("store/")
a4 = a3[0]
if a2_store in a2:
print ('a2')
except:
if a4 not in a1:
print ('a1')

如果在执行a4 = a3[0]之前引发异常(例如,如果a3[0]引发IndexError(,则a4确实没有定义。

相关内容

最新更新