How will Triangle objects know which __init__ method to use?

Question

How will Triangle objects know which init method to use?

Answer

In this exercise we’ve defined a new class called Triangle that inherits from the base class Shape. Both classes define an __init__() method! Luckily, Python is smart enough to see that we intended to use the initialize method in our Triangle class when creating a new Triangle object.
Python checks the object that called the method and sees that it has a method of the same name that then takes priority over the one in the base class.
In the case of __init__() being defined twice, it sees that we’re initializing a Triangle, so we use that method.

4 Likes

Any chance we could expand/simplify this?

1 Like