[Tutor] Impossible Else as Exception

Danny Yoo dyoo at hashcollision.org
Wed Apr 10 02:17:41 CEST 2013


>> if condition is 1:
>>      do something with 1
>> elif condition is 2:
>>      do something with 2
>> else: # Impossible unless the code above is flawed.
>>      Raise Exception
>
>
>
> The above isn't a great example, because the sample code *is* flawed.
> The short reason why it is flawed is this:
>
> You use identity tests (using `is`) instead of equality tests (using
> `==`) which makes the above code fragile and dependent on the exact
> version and implementation of Python being used.


I don't think we can say this.  The code above is not literal.
Otherwise, we should be technically obligated to raise the exact same
objection to "do something with 2".



Going back to the original question: perhaps an assertion here would
be sufficient.  Something like:

    assert False, "Impossible situation"

would document that the case analysis above should be technically
exhaustive, and that reaching the assertion should be treated as an
internal error.


More information about the Tutor mailing list