Object-oriented programming
Python is a flexible language that can be written using various programming styles. Different paradigms can be used in different parts of the same code to suit specific needs.
So far in this course, we have focused on procedural programming. In this section, I will briefly talk about the various programming paradigms in Python, then focus on a very common style: object-oriented programming.
Programming paradigms in Python
Imperative programming
In imperative programming, the code follows an execution flow and the state of a program changes throughout its execution.
This is the paradigm most commonly used in Python and the one you are most likely to be familiar with.
Procedural programming
Procedural programming is a subtype of imperative programming widely used for simple, top-down sets of instructions. The focus is on procedures (functions) that operate on data.
Small scripts, task automation, and data analyses often rely on procedural programming. This is what we have covered in this course so far.
Object-oriented programming
In object-oriented programming (OOP), the code is organized around objects which are instances of classes and bundle together data (attributes) and behaviour (methods). This reduces the amount of code that needs to be written thanks to the property of inheritance. It also hides parts of the code that doesn’t need to be exposed.
Web applications, software development, and complex code such as can be seen in deep learning rely on OOP, making it probably the most common programming style in Python outside of data science.
Declarative programming
Declarative programming does not rely on state changes or control flow, but instead on formal logic.
Functional programming
Functional programming is the subset of declarative programming that can be used in Python. It relies on pure functions without side effects.
This is a restrictive style, but it has the benefits of being more readable, easier to maintain, and promotes better code modularity and reusability.
If you ever want to use JAX—a Python library for high-performance array computing, you will have to write functional programming code.