What Are Classes in Python? Classes are the foundation of object-oriented programming. Classes represent real-world things you want to model in your programs: for example dogs, cars, and robots. You use a class to make objects, which are specific instances of dogs, cars, and robots. Title: Python turtle module cheatsheet Cheat Sheet by NatalieMoore - Cheatography.com Created Date: 0627Z.
Class inheritance is a very useful Object-oriented Programming construct for sharing and reusing code. Inheritance makes it possible to break up and organize your code into a hierarchy, from generic to specific. Objects that belong to classes that are higher up in the hierarchy (more generic) are accessible by subclasses, but not vice versa.
Earlier, we saw that bool
is a subclass of int
, thus, it inherited the properties and methods of the int
class, and then extended it to be more specific to booleans.
We can do the same with our own classes, too. In a file called vehicle.py
, let’s create a parent Vehicle
class, and have our Car
class be a subclass.
When we instantiate a Car instance, the interpreter calls __init__()
, where we pass in two arguments (make
and model
) and an optional 3rd (fuel
, which defaults to “gas”). In __init__()
, we call super().__init__()
, which resolves to our parent class, Vehicle
, and runs its__init__
function, where the variables are stored. Note that even though the variables are stored at the Vehicle
level, they are instance variables because self
is bound to my_car
, which is a Car
, which is a Vehicle
. Don’t forget to import your Vehicle and Car classes. Behold:
Python Oop Cheat Sheet
Overriding Variables in a Subclass
We can, of course, use a subclass to override variables that belong to a parent class. Let’s update our vehicle.py
file:
Note how our Truck’s class variable number_of_wheels
overrode the parent class Vehicle
’s number_of_wheels
(or, to be more specific, the interpreter found number_of_wheels
in a closer scope, the Truck
class, and did not need to continue searching up the hierarchy). Likewise, Motorcycle
overrides just the number_of_wheels
variable to equal 2. Notice there is no __init__()
function in Motorcycle
- the number_of_wheels
variable is overridden but instantiating a Motorcycle just goes straight to the Vehicle.__init__()
method.
Multiple Inheritance in Python
Python Oop Cheat Sheet Download
Can Python classes inherit from multiple parent classes? Yes, this is called multiple inheritance. It’s not as commonly used for simple programs, but you’ll see it more often as you start using libraries.
One common use case for multiple inheritance in Python is for a type of class called a Mixin. Mixin classes tend to be used to quickly and easily add additional properties and methods into a class. This type of design pattern encourages code with composable architecture.
Python Oop Cheat Sheet Examples
Unfortunately, we won’t have time to cover the topic of Multiple inheritance in this workshop… because it’s out of scope. 🤣