I was looking at the documentation on exceptions in python :-( https://docs.python.org/2/tutorial/classes.html#exceptions-are-classes-too )
I can not find how this code works
class B:
pass
class C(B):
pass
class D(C):
pass
for c in [B, C, D]:
try:
raise c()
except D:
print "D"
except C:
print "C"
except B:
print "B"
Conclusion: -
B
C
D
What happens in the classes? Just by going through the documentation that I can get, objects of all classes are created (the order of creating objects: - B, C and D), and exceptions are expressed in their names if this order then explains the output of B, C, D.
But if we replace "excpet" B with "except D", the whole result will be changed.
class B:
pass
class C(B):
pass
class D(C):
pass
for c in [B, C, D]:
try:
raise c()
except B:
print "B"
except C:
print "C"
except D:
print "D"
Conclusion: -
B
B
B
Now it makes my head spin: /
How does the order βexcludeβ a change in output?
I know that I have something missing from the documentation, perhaps because it is not very clear :(