コードは次のとおりです。
aList = []
for p in anotherList:
aList.append(p)
try:
k=p.someMethod()
aList.append(k) #getting error here
except someException:
continue
return aList
「グローバル名エラー -- aList が定義されていません。なぜですか?
が定義されていない場合は、try-except句に入る前に、ループの先頭でaList
エラーを受け取っているはずです。タイプミスはありませんか?aList.append(p)
aList.append(k)
aList = []
for p in anotherList:
aList.append(p) # <== should have gotten error here first!
try:
k=p.someMethod()
aList.append(k) #getting error here
except someException:
continue
return aList
このコードに問題はありません。
>>> anotherList = [1, 2, 3, 4, 5]
>>> aList = []
>>> for p in anotherList:
... aList.append(p)
... try:
... aList.append(9)
... except someException:
... continue
...
>>> aList
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9]
ご覧のとおり、動作します。
それでも問題が解決しない場合は、コードをさらに投稿してください。投稿した部分にエラーはありません。