vendredi 21 juillet 2017

Weird behavior of if else statement after try block

I have a try block in a for loop which is followed by an if else statement like this:

        for object in list_of_objects:
            if test_some_condition(object):
                try:
                    object.list_attr.remove(val)   #here 'list_attr' is a list
                except ValueError:
                    dummy=0 #this is where 'dummy' gets first instantiated
        if some_other_condition:
            some_list=makelist(True) #this is where 'some_list' gets first instantiated
        else:
            some_list=makelist(False) #this is where 'some_list' gets first instantiated
        maxval=np.max(some_list)

I was doing some debugging, so I didn't really care what the except part of the statement did, I just had my breakpoint set there. But when I stepped through the program, after going through the for loop, it jumped straight from the if line down to the last line. Since some_list was never instantiated it throws an exception. I have no idea what could be causing this.

Then I changed the line in the except branch to print('Something went wrong') and once I did this, the if-else statement all of a sudden executed properly. I am still curious though what could have caused my initial problem? Thanks for any answers!

Aucun commentaire:

Enregistrer un commentaire