Object-Oriented Programming


Python is an object-oriented programming language—this means that in Python, everything is an object! In this chapter, you will see what this means and how you can use it in a practical way. Start by looking at a few objects from everyday life, such as pens, books, smartphones, computers, etc.

Very different objects!
Very different objects!

Objects can have many different shapes and characteristics, but you can classify different versions of the same object into a category or group. That's why it's easy to recognize a chair in a store, for example, although its appearance (shape, color, etc.) can vary greatly from one model to another.

It is by observing the common points between different objects that you are able, mentally, to classify the objects in the same group or category!

For example, there are different types of books, but they all have a title, an author, a back cover, etc. All books share different attributes that let you classify them in a well-identified category: books.

Classes: Object Models

In programming, this concept of a group or category of objects is called class. A class can be considered as the construction diagram for an object that will define the characteristics of all objects of this type and their features. From this class, you will be able to create different models of an object.

Let's take a concrete example with a Car class. The plan of a car can be defined by:

  • its characteristics, called attributes: it must have four wheels, a color, a shape, an engine power, etc.

  • its functionalities, called methods: it can drive, brake, etc.

So, from this plan, you can create different car models:

  • An ordinary family car, green, medium power (110 hp)

  • A sports car, red, relatively powerful (180 hp)

  • A small blue city car, not very powerful (90 hp)

  • etc.

And no matter what the car model, they are all capable of driving or braking, but not with the same performance!

In summary, a class is the outline of an object, defining its attributes and methods. From the same class, you can therefore create several objects of the same type, but with different attributes—these are called class instances.

Focus on Methods

As we said before, in Python, everything is an object. This means that, without knowing it, since the beginning of this course, you have been manipulating objects! Consider the following lines of code to illustrate this:

var1 = 14
var2 = 1031

Here, you have declared two variables named  var1  and  var2  containing the values 14 and 1,031. In reality, you have created two instances of the int class, two objects each with a single attribute: its value. The same is true for floats or strings: every time you create a variable of one of these types, you are actually creating objects in Python with the value you assign to them as an attribute.

Up until now, we have talked about attributes, so now it is time to see what methods are. A class method is a function that is only available for the instances of this class. If, for example, we consider the Car class presented above having a drive() method, and a Plane class having a fly() method, you will agree quite logically that a plane can’t drive, and a car can’t fly. The same goes for our various objects!

The use of a method is always done via the   variableName.method()  notation. For example, strings have a method called   lower()  which will transform all the text contained in an object into lower case. Here's how to use it:

The string method: lower
The string method: lower

In the same way as with functions, class methods can take parameters.

String Methods

During the various data analyses that you will have to perform, you will inevitably be confronted with textual variables at some point. You have already seen how to change your string to lowercase, but you may also need to replace some specific words, format the text in a certain way, etc.

Python has implemented many methods to allow us to do all this. Here are the most common ones:

  • upper() :  returns the whole text in upper case.

  • capitalize() :  returns the whole text in lowercase with the first letter capitalized.

  • replace(old, new) :  this method takes two arguments: old and new, both of which are strings.  The method returns the original string with all occurrences of old replaced with new

  • find(string)  returns either the index of the first occurrence of the string passed in the  argument, or -1 if it does not find it.

Here are some examples of how these methods are used:

String Methods
String Methods

As you can see here, especially with the lines concerning the  a  variable, the methods seen above do not modify the initial object! They only return the result of the method applied to the object. You will regularly have to reassign this result to the initial variable, when you want to modify it directly.

Try it for Yourself

Manipulate objects yourself in the following exercise.

You can find the solution here.

Let's Recap

  • class is a construction plan for an object.

  • A variable is an instance of a class, or an object.

  • An object is defined by its attributes.

  • All instances of a class have access to the same methods via the.  (dot) notation.

  • A method, like a function, generally does not modify the initial object.

In the next part, we will see in more detail how to organize your code via different structures and complex objects.

Post a Comment

Previous Post Next Post