Book Image

Python Fundamentals

By : Ryan Marvin, Mark Nganga, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Nganga, Amos Omondi

Overview of this book

After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions. As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation. By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.
Table of Contents (12 chapters)
Python Fundamentals
Preface

Multiple Inheritance


Multiple inheritance is a feature that allows you to inherit attributes and methods from more than one class. The most common use case for multiple inheritance is for mixins. Mixins are classes that have methods/attributes that are meant to be used by other functions. For example, a Logger class would have a log() method that writes to a logfile, and when added to your classes as a mixin, would give them that same capability.

The following is the syntax for multiple inheritance:

class Subclass(Superclass1, Superclass2):
    pass

The subclass inherits all of the features of both superclasses.

Exercise 45: Implementing Multiple Inheritance

In the real world, lions and tigers can naturally mate to create a hybrid known as a liger or a tigon. Ligers are much larger than either lions or tigers, they are social like lions, have stripes, and, just like tigers, they like swimming. We're going to create a Liger class that inherits from both the Lion and Tiger class we're going to...