How will Triangle objects know which __init__ method to use?


#1

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.


FAQ: Learn Python - Introduction to Bitwise Operators - Inheritance Syntax