r/pythonhelp • • May 09 '22

How is "B, C, D" printed?

class B(Exception):

____pass

class C(B):

____pass

class D(C):

____pass

for cls in [B, C, D]:

____try:

________raise cls()

____except D: print("D")

____except C: print("C")

____except B: print("B")

The underscores are spaces.

2 Upvotes

9 comments sorted by

1

u/Goobyalus May 09 '22

For future reference you can format a code block by adding an additional 4 spaces before each line, and leaving a blank line before and after the code block. I believe there is a button for this in the Reddit entry box. You could also indent your code block in your text editor by highlighting it and TABing before copying, and undoing to put your code back how it was.

1

u/Mattbown7 May 09 '22

I should clarify that, “B, C, D” is printed from this code. I don’t understand how. I can’t seem to follow the logic.

1

u/Goobyalus May 09 '22
  1. Do you understand for cls in [B, C, D]:?

  2. Do you understand try/except?

  3. Do you understand class inheritance, and the declarations of classes B, C, and D?

  4. Were you expecting the code to do something different, or are you just lost?

1

u/Mattbown7 May 09 '22

I think it’s 3 and 2.

Specifically, does class C(B):… mean that then entire class is passed as an argument to class B?

And does raise cause an exception, and that exception is what triggered the except statement to print?

Sorry, haha This is my first class in python

1

u/Goobyalus May 10 '22 edited May 10 '22

Ok cool.


A class is like a definition for a type of object. "Class" the way "sedan" and "minivan" are classes of vehicles. So a class defines the properties and functionality that "instances" of that class have.


Classes can be specialized farther. Let's say we take the class of "trucks." We can imagine subclasses of "pickup truck," "tractor truck," "dump truck," etc.

Two things to note:

  1. A pickup truck is a truck, but a truck isn't necessarily a pickup truck.

  2. A pickup truck, being a truck, has the normal properties that apply to all trucks, but also some specialization that makes it a pickup truck


In Object Oriented Programming, we can define subclasses. Subclasses inherit everything from their parent, and let you specify new things, or override things from the parent.


The syntax in the declaration class C(B): means that class C "inherits" fromB. I.e. C is a subtype of B. So an instance of C is also an instance of B, but an instance of B is not an instance of C.


This code defines classes that inherit as follows: D > C > B > Exception > ... . All of them are Exceptions, all but Exception are Bs, etc.


After declaring a class, we create an instance of the class by calling it. For example, B is the class itself, and B() creates, initializes, and returns an instance of that class.


Yes, raise is the keyword for raising an exception (like "throw" in Java). When we do

raise cls()
  1. cls is one of the classes B, C, or D.

  2. cls() creates an instance of whichever class cls is at the time (which changes because of the loop)

  3. That instance, which is an Exception because of inheritance, gets raised

  4. Whichever except clause matches the subtype of Exception first handles the exception


Try reordering like this and rerunning:

    except B: print("B")
    except D: print("D")
    except C: print("C")

Do you understand what happens there?

2

u/Goobyalus May 09 '22

OP's code formatted:

class B(Exception):
    pass

class C(B):
    pass

class D(C):
    pass

for cls in [B, C, D]:
    try:
        raise cls()
    except D: print("D")
    except C: print("C")
    except B: print("B")

Output of OP's code:

B
C
D

1

u/COLEMMANDO May 09 '22

Print.raise.cls() If that’s not correct try this. Print(raise.cls())

1

u/skellious May 09 '22
class B(Exception):

    pass

class C(B):

    pass

class D(C):

    pass

for cls in [B, C, D]:

    try:

        raise cls()

    except D: print("D")

    except C: print("C")

    except B: print("B")

The underscores are spaces.

no, the spaces are spaces. underscores mean something else in python.

(use four spaces at the start of every line for a codeblock. add four more spaces for each level of indentation.)

1

u/Goobyalus May 09 '22

Please clarify your question. What do you expect this to do? What do you understand about wat it does?