User Tools

Site Tools


programming-languages:python:start

Python

Python is an object-oriented programming language with a simple and easy to learn syntax which emphasizes readability and therefore reduces the cost of program maintenance.

Inheritance

With a single inheritance, the use of super() allows to access to a method in the parent class:

class Parent(object):
    def __init__(self, x):
        print(x)
 
 
class Child(Parent):
    def __init__(self, x, y):
        super().__init__(x)  # same as: Parent.__init__(self, x)
        print(y)
 
 
Child('parent', 'child')
# it prints:
# parent
# child

It also allows to call all the inheritated methods in the correct order:

class GrandParent(object):
    def __init__(self, x):
        print(x)
 
 
class Parent(GrandParent):
    def __init__(self, x, y):
        super().__init__(x)
        print(y)
 
 
class Child(Parent):
    def __init__(self, x, y, z):
        super().__init__(x, y)
        print(z)
 
 
Child('grandparent', 'parent', 'child')
# it prints:
# grandparent
# parent
# child
programming-languages/python/start.txt · Last modified: 2020/04/03 09:36 (external edit)