Object-oriented programming with Python: core principles and examples

Naomy Duarte Gomes
5 min readSep 11, 2022

--

Object-oriented programming (OOP) is a very efficient approach to writing software. Its main difference concerning procedural programming is that POO has procedures applied to each object instead of globally, allowing the code to be more independent and therefore reusable.

Introduction

When it comes to programming languages, they are classified according to their features, according to what they allow and do not allow us to do. This classification is called paradigms. There is the procedural paradigm, where the procedure is a sequence of instructions that will execute following the steps informed. The purpose of this paradigm is to dictate sequences of actions that will be executed in an orderly manner and produce the desired result. It is simple, straightforward, and has a code that's easy to follow, but it’s not reusable. The object-oriented paradigm, on the other hand, defines procedures that are applied to specified objects, such that each part of the code will run according to them, thus allowing reusability.

In the OOP we have two important concepts to be defined: class and object. A class is a set of properties that is common to certain types of objects. For example, we can define a class that will cover all the dogs, which are animals with well-developed noses, four legs, a tail, and that bark. The objects of this class are the dogs, which have in common all these characteristics but can have specific characteristics themselves, such as the color of the furr, the size, etc. Objects are the basic unit of OOP.

Fig. 1: Creating the Dogs class, its attributes and methods, and the object matilda.

In fig. 1 we can see the creation of the Dogs class. It has attributes and methods that will be common to all objects. The attributes are the data relevant to the class. Methods are functions that belong to a class and can only be called by objects.

At the bottom of the code, the object ‘matilda’ is created and contains the name, the age, and the color of the furr that are characteristic of it (characteristic of the dog, in this case). We also call the objects instances of the class.

Now that we know what are classes and objects, we can talk about the core principles of OOP, which are: encapsulation, inheritance, polymorphism, and abstraction.

Encapsulation

Encapsulation is the process of making a certain part of the code private. It encapsulates variables within a class making them inaccessible outside the class.

Fig. 2: Creating the BankAccount class and defining its attributes and methods.

In fig. 2, we create the BankAccount class with a private attribute “number”. If one tries to access this private attribute (print(account1.__number)), it will print an error, as opposed to public attributes such as the name. The retrieve_balance method will return the balance of the account.

Inheritance

Inheritance is the process of making a new class based on an existing one. When we construct a subclass/child class that has the same attributes as another superclass/parent class (and possibly new ones), we can make the first inherit the latter.

Fig. 3: Creating the child class CrediCard that inherits the BankAccount attributes and methods, but can also define new ones.

In fig.3, the child class CreditCard inherits the parent class BankAccount. We define this inheritance by writing Childclass(Parentclass) and the super() function. As we can see, the child class will have all the attributes and methods of the parent class plus the “limit” attribute and the new method get_limit that returns the available limit of the credit card.

Polymorphism

Polymorphism happens when child classes alter/override the behavior of the methods they inherit, adapting them to their necessities. Therefore, one method can have more than one structure and functionality.

Fig. 4: The child class overrides the method retrieve_balance to better adapt its necessities.

In fig. 4, the inherited method retrieve_balance of CreditCard was altered to return the balance, the limit, and the total that can be spent by the client considering the balance of the account + limit. This method is polymorphic as it has different functions in the parent class and the child class.

Abstraction

Abstraction happens when we create an abstract class that will not have its attributes and methods implemented, meaning that it cannot be instantiated. Therefore, if we want to instantiate its methods and attributes, we will need to create a child class. In Python, we create an abstract class by using the ABC (abstract base class) module.

Fig. 5: Creation of an abstract class Books that cannot be implemented and its child class NovelBooks that can be implemented.

In fig. 5, we create the abstract class Books which is telling other possible child classes that they will have the attributes title, author, price, and discount, a property that returns the information on the book, and a method to calculate the price considering a discount. Next, we create the child class NovelBooks with the same properties as the abstract class but that can now be instantiated. If we try to instantiate the abstract class, we will receive the message “TypeError: Can’t instantiate abstract class Books with abstract method get_price”.

You may now be wondering why use abstract classes. From the example above, we can see it is useful when we want to define a structure for other classes to follow, therefore ensuring consistency in how other classes will be implemented. This can be a good practice when working in a team, to ensure a pattern for subclass creation.

Conclusion

We can see how efficient OOP can be. It allows us to reuse code and define structures that must be followed, ensuring consistency. We can define many classes and subclasses as we think necessary, with the advantages offered by the use of inheritance and polymorphism. It is also possible to assure the security of defined parameters by the use of encapsulation.

--

--

Naomy Duarte Gomes

PhD in Physics, MBA in Data Science and Data Scientist writing about data.