Object-Oriented Programming
-
Python Mixins
The object-oriented concept of a “mixin” is used in Python, as in many programming languages (but not all). The Python concept of a mixin doesn’t quite literally conform to the Wikipedia definition of a mixin, but it’s close. Usually a subclass is a version of the superclass. In the example below, a VW literally is…
-
Multiple Inheritance
It’s possible for a class to inherit methods from multiple superclasses. Typically, a subclass IS a kind of the superclass. For example, if Tiger is a subclass of Cat, this is because a “Tiger” literally is a kind of “Cat“. In this example, a SmartPhone is a Phone, but it’s also a Camera. Class Hierarchies…
-
Inheritance
It’s possible to create classes that are similar to existing classes, but add or change some methods. In this example, we create a Cat class with a speak method. We then create a Tiger class that’s a subclass of Cat. We add a roar method to Tiger. Tiger objects can then do everything that Cat…
-
Class Variables and Methods
In Python, every object has its own separate copy of a field or instance variable. On the other hand, it’s also possible to define class variables. In this case, all objects share the same copy of the class variable. Class variables may be referenced via self, but are usually referenced via the name of the…
-
Classes
In object-oriented programming (OOP), functions are bundled with data into classes, which are used to create objects. Here is the simplest example of a class possible in Python. A class is defined using the class keyword, and the pass keyword is used to indicate the class has no body. Even so, it is possible to…