0

Below is an example piece of code from the python documentation dealing with exception handling and classes. I am working to understand both of these, and my background is in Java, so I am used to strongly typed classes and variables. Would someone please explain in detail what is going on here?

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"

Are the first six lines actually creating instances of B, C, and D or just creating a class type. If they are just class types, I assume they are instated in for c in [B, C, D]:. How can instances of classes C and D be instantiated without parameters? Then what is going on with the exception stuff?

3 Answers 3

1

The first six lines create classes. I like to think of them as definitions of instances that will later be created. Using for c in [B, C, D]: does not instantiate anything; it just assigns c to each of B, C, and D which are still just classes. For the first iteration, c is the same thing as B. Therefore, when you say raise c(), c() makes an instance of B and raises it as an exception. That exception is caught in the except B: block and causes the print "B" line to execute. For the next iteration, c is assigned to the next value in [B, C, D]: C. Now, raise c() makes an instance of C and raises it as an exception. That exception is caught in the except C: block and the print "C" line is executed. The same thing happens with D.

Sign up to request clarification or add additional context in comments.

Comments

1

The first six lines define the classes. They are instantiated without parameters because e.g. class C(B): means that class C inherits from B, not that instantiation of C requires passing an argument. This is explained in the documentation on classes.

Each time through the loop, the interpreter will raise an error of the given class. It will print whatever type of exception class is raised, with B for B and so on. The output, as you see when you run that code, is B, then C, then D, because those are the classes of exception that were raised, in that order, as you see in [B, C, D] over which the loop iterated.

Comments

0

The first six lines are creating the class types.

Since everything in python can be assigned to a variable, this includes functions, classes (not just instantiated objects of that class) for c in [B, C, D]: is doing.

Lastly what the exceptions are doing first in the try: it tries to raise an exception of type c by creating an instance of it. Thus it first raises an exception of type B then type C then type D.

The excepts are there to catch those exceptions, for each type. In python 2 any object can be raised as an exception, this is different from python 3 where all exceptions that are raised must derive from type BaseException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.