Reputation: 1
Take the pseudocode below for example:
Python keeps the previous value for x
, so if get_sum()
fails, the conditional is still checked using the previous value of x
.
Is this because python for loop doesn't introduce a new scope and is it ok to simply del the object at the end of each iteration?
for number in number_list:
try:
x = get_sum()
except:
....
if x > 100:
do something
Upvotes: 0
Views: 707
Reputation:
Every variable in python is created in the scope of their respective functions and classes rather than at different levels of indentation, or in for loops or while loops. There's no block scope in python like there is in java.
If you need x to not retain its old value, you can always set x = None
in your except clause and have a conditional catch it later on. If i'm misinterpreting your question please leave a comment
Upvotes: 2