avatarSamer Sallam

Summary

The web content provides an introduction to Object-Oriented Programming (OOP) in Python, detailing its necessity, definition, advantages, and practical examples for complex real-world problems.

Abstract

The article serves as the second part of a complete course on Object-Oriented Programming (OOP) in Python, emphasizing the evolution from primitive data types to the creation of complex data types that mirror real-world entities. It outlines the limitations of primitive data types for modern programming challenges and introduces OOP as a solution. The concept of OOP is explained through its ability to combine data properties (attributes) and functions (methods) into a single entity known as a class. The article also demonstrates the practical application of OOP with an example of a bank account application, illustrating how OOP facilitates code reusability, maintainability, and increased developer productivity. Furthermore, it highlights the ease of representing real-world entities in software solutions using OOP principles. The article concludes by summarizing the key points learned and invites readers to subscribe for future posts and consider Medium membership for unlimited access to stories and support for writers.

Opinions

  • The author suggests that OOP is essential for addressing complex programming problems that cannot be efficiently handled by primitive data types alone.
  • OOP is presented as a programming paradigm that enhances code reusability and maintainability, leading to increased productivity for developers.
  • The article posits that OOP simplifies the process of translating real-world problems into software solutions by allowing the creation of classes that encapsulate both data and behavior.
  • The author expresses a belief in the value of OOP education, encouraging readers to follow the series and engage with the content by subscribing and considering Medium membership.
  • The importance of OOP is underscored by its ability to create reusable code patterns, which can save time and reduce the amount of code needed to solve a problem.

Introduction to OOP: Python OOP Complete Course — Part 2

Photo by Semyon Borisov on Unsplash

Before we start let me tell you that:

  • This article is a part of The Complete Course in Object Oriented Programming in Python which you can find it here.
  • This article is also available as a YouTube video here.

Introduction

First of all, let me ask you:

Are wondering why you have to learn OOP? If the answer is yes, let me tell you that this article is going to answer exactly this question.

This article will cover the following outlines:

  1. What we Have Before OOP
  2. What Is OOP?
  3. OOP Advantages

1. What we have Before OOP

In the past, when programming languages came along, they supported what we call primitive data types or simple data types like integer, float, Boolean, etc… These types actually are very good for simple programs that require for example using arithmetic operations only.

a = 1
a = a + 2

However, nowadays we have more complex problems which require representing and using more complex data types that come from our real-world like a car, an employee, or a bank account which cannot be represented efficiently by using only the primitive data types.

To handle the previously mentioned issue, OOP has come to the scene, so what is OOP?

2. What Is OOP?

Simply OOP is just a programming style in which developers can create complex data types by combining two things:

  • Data properties that describe the new type.
  • Functions that process these properties.

In other words, OOP gives developers the flexibility to create new types by connecting the data and the functions that process this data.

In the OOP context, we call:

  • The new data type we want to create is a class.
  • The data properties associated with these types, attributes.
  • The functions that handle these properties, and methods.

These three words are very common and you will see them frequently in later articles.

To make sure that the idea is clear, let me share the following example with you:

Suppose I want to develop an application for processing transactions in a bank. The most important data type, in this case, is the bank account. To define this data type, you have to do the following:

  • Give this data type a name, for example, bank account.
  • Define the properties that describe this new data type, and in this case, we have for example account holder's name, account ID and the balance in this bank account.
  • Define the actions that process these attributes. For example, when I have a bank account, I can withdraw money, or I can deposit money. Refer to Figure 1.
Figure 1: Define Bank Account (Image By Author).

So far we have just defined the new data type that represents the bank account, but how to use it? let’s see...

You have to define an instance from this bank account, which we call an object.

For example, if we have an integer data type, we define an instance of an integer by assigning an integer number to a variable.

And here we are doing the same thing. But, when we define an instance of an object from any class, we have to specify values for the attributes that this data type has.

Once I have an object from this data type, I can do some actions on this object. For example, I can withdraw some money from my account or I can deposit some money into my account. Simply, I can do this by using the methods: withdraw and deposit. Refer to Figure 2.

Figure 2: Bank account class and object (Image By Author).

After we learnt what OOP is, let’s talk quickly about some OOP advantages.

3. Why OOP Is Important

Photo by Waldemar Brandt on Unsplash
  • OOP style focuses on creating reusable code patterns because in OOP we create our data types once and then we are using them again and again. Therefore, you can save a lot of time because you are reusing the same code.
  • OOP programs are easy to be maintained: since your code becomes reusable, you have to write fewer lines of code to solve a problem which in turn decreases the maintenance time.
  • OOP increases developers’ productivity because it allows programmers to easily represent real-world entities in computer programs. Therefore, OOP makes the process of translating our problems into software solutions simpler and quicker.

Now, let us summarize what we have learned in this article:

Photo by Ann H on pexels
  • Primitive data types are limited and insufficient to solve complex real-world problems.
  • OOP enables developers to create complex real-world entities by binding together: the Data Properties (Attributes) and the Functions (Methods) that process these attributes.
  • We call the data type that’s created in OOP a class.
  • We can create multiple instances from this class, and each instance is called an object.
  • OOP helps developers to write reusable and maintainable code.

P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:

  • First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
  • Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membership which only costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.

To get back to the previous article, you can use the following link:

Part 1:OOP Basics Overview

To move on to the next article, you can use the following link:

Part 3:Introduction to Classes and Objects

Object Oriented
Python
Programming
Oop Concepts
Introduction
Recommended from ReadMedium
avatarAbhay Kumar
OOPs in Python

An easy guide

10 min read