Object Oriented Programming
Inheritance in Python
Inheritance is used to inherit another class' members, including fields and methods. A sub class extends a super class' members.
Syntax
class SubClass(BaseClass):
def some_method(self, passed_params):
#calling base class version of method
BaseClass.some_method(self, passed_params)
Notes
Sub classes inherit methods and members from the base class. If a subclass has a method with the same name as the base class, the subclass' method overrides the base class method.
Example
class Cat(object):
def __init__(self, name):
self.name = "Garfield"
def sound(self):
return "Meow"
class Tiger(Cat):
def __init__(self, name): #will overwrite Cat constructor
self.name = "Mike Tyson Jr."
def sound(self): #will overwrite the Cat sound method
return "Roar"
def cat_sound():
Cat.sound() #will call cat's sound