Python Inheritance



Classes can derive attributes from other classes via inheritance. The syntax goes:
class DeriveClass( BaseClass):
    <statement 1>
    <statement 2>
      ...
    <last statement >

 
If the base class is present in other module, the syntax for derivation changes to:
class DeriveClass( module.BaseClass):
Python supports overriding of base class functions by derive class. This helps in adding more functionality to be added in overriden function in derived class if required.
Python supports limited form of multiple inheritance as:
class DeriveClass( BaseClass1, BaseClass2, ...):
    <statement 1>
    <statement 2>
      ...
    <last statement >

 
where an attribute if not found in DeriveClass are then searched in BaseClass1 and their parent then BaseClass2 and their parents and so on. With new style, Python avoids diamond problem of reaching common base class from multiple paths by lineraly searching base classes in left to right order.

No comments:

Post a Comment