In the above example, notice that 
__init__() method was defined in both classes, Triangle as well Polygon. When this happens, the method in the derived class overrides that in the base class. This is to say, __init__() in Triangle gets preference over the same in Polygon.
Generally when overriding a base method, we tend to extend the 
definition rather than simply replace it. The same is being done by 
calling the method in base class from the one in derived class (calling 
Polygon.__init__() from __init__() in Triangle).
A better option would be to use the built-in function 
super(). So, super().__init(3) is equivalent to Polygon.__init__(self,3) and is preferred. You can learn more about the super() function in Python.
Two built-in functions 
isinstance() and issubclass() are used to check inheritances. Function isinstance() returns True
 if the object is an instance of the class or other classes derived from
 it. Each and every class in Python inherits from the base class object. 
No comments:
Post a Comment