jeroeningelbrecht

View on GitHub
created at: 9 August 2019
[home]

Inheritance

by jeroen

Inheritance

What defines what object component is being executed

Python looks for objects components in the following order:

class SuperClass1:

    def speak(self):
        return 'SuperClass1 is speaking'


class SuperClass2:

    def speak(self):
        return 'SuperClass2 is speaking'


class SubClass(SuperClass1, SuperClass2):

    def interact(self):
        print(super().speak())


sub = SubClass()
sub.interact() # prints 'SuperClass1 is speaking'

Polymorphism

class One:
    def do_it(self):
        print('do_it from One')

    def do_anything(self):
        self.do_it()


class Two(One):
    def do_it(self):
        print('do_it from Two')


one = One()
two = Two()
one.do_anything()   # 'do_it from One'
two.do_anything()   # 'do_it from Two'

the situation in which the subclass is able to modify its superclass behavior (just like in the example) is called polymorphism.
The word comes from Greek (polys: “many, much” and morphe, “form, shape”), which means that one and the same class can take various forms depending on the redefinitions done by any of its subclasses

Baseclass(es) and Subclass(es)

There are ways to get the baseclass(es) (superclass(es)) of a class and to get the classes that inherit from one particular class (the subclass(es)).

Discover the baseclass(es)

code: __bases__
scope: class

example
see below

Discover the subclass(es)

code: __subclasses__()
scope: class
example

class SuperClass1:

    def speak(self):
        return 'SuperClass1 is speaking'


class SuperClass2:

    def speak(self):
        return 'SuperClass2 is speaking'


class SubClass(SuperClass1, SuperClass2):

    def interact(self):
        print(super().speak())


sub = SubClass()
sub.interact() # prints 'SuperClass1 is speaking'
print(sub.__class__.__bases__)

superC = SuperClass1()
print(superC.__class__.__subclasses__())
[home]
tags: [python] [inheritance]