Learn Python object-oriented programming (OOP) with a Raspberry Pi

By Russell Barnes. Posted

Discover Python object-oriented programming (OOP) on a Raspberry Pi

In Python (and other Raspberry Pi programming languages), almost all of the code you will encounter is created in a style called ‘object-oriented programming’, or OOP for short.

If you grew up with OOP, it is the obvious way to create computer programs.

The code is used to create objects. These represent real-world things: a dog, a chair, or the wheels on a car.

Objects contain both the variables that make that thing: a person’s height and age, a wheel's rotation speed, or a dog's name for example. They also contain the functions that object can perform: a dog can jump, or walk, or run; a wheel can rotate faster or slower.

OOP bundles both the variables and functions together. This style makes it super-easy to cut and paste the code from one program to another. You don’t even need to cut and paste, in fact: you use import statements to get functions and the variables they need.

With object-oriented programming, you don’t need to create an object for a wheel: you just find one somebody else has made and import it to your program. It'll come pre-packaged with all the variables a wheel needs: diameter, rotation speed, air pressure, and so on.

This article is based on Learn Object-Oriented Programming in issue 54 of The MagPi. Issue 53 of The MagPi has a companion piece on learning to code with a Raspberry Pi

See also:

Learn OOP using Python on a Raspberry Pi

At the start of most programs, you’ll find a bunch of import statements. These are used to paste in code which has been created by other people.

It isn’t perfect. The style can be accused of overkill. “The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them,” says Joe Armstrong, creator of Erlang. “You wanted a banana but what you got was a gorilla holding the banana, and the entire jungle.”

More troubling, especially for beginners, is the whole bunch of decorative terminology surrounding OOP. You’ll encounter lots of strange words like ‘encapsulation’ and ‘instantiation’.

These make the concept appear much more complicated than it is, and can also be rather off-putting to newcomers.

So OOP is a bit wordy and lends itself to navel-gazing. Many makers, hackers, and coders struggle to understand OOP, and indeed you can get a long way without understanding it.

Young coders, on the other hand, are increasingly introduced to programming via Scratch.

Teaching objects by stealth with Scratch

 The sprites in Scratch are stealthy objects

Software like Scratch is included with Raspbian with PIXEL (and Debian with PIXEL) and is designed specifically to teach students OOP stealthily.

In Scratch, objects are called ‘Sprites’. They resemble video game characters. The idea is that children brought up on Scratch will inherently feel at home with objects when they migrate to a language like Python.

Beyond procedure to objects

When you first start programming, you’ll begin by writing procedural code.

In good old-fashioned procedural programming, you typically create all your variables at the start of a program. Then you make some function definitions (these are blocks of reusable code).

OOP takes all the building blocks of procedural programming – variables, functions, loops, conditions – but bundles them into self-contained blocks.

Most coders create procedural scripts that import objects (from modules and packages). So you’re using objects without even realising it.

OOP concepts are found in almost all modern programming languages, including Python and Java.

In The MagPi 54 we included a feature on OOP that combines Scratch with Python to explain OOP concepts.

It's the simplest guide to object-oriented programming we could create. We will be adding various Scratch and Python tutorials explaining OOP to this article. So bookmark this page and come back to start Learning OOP.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.