avatarSamer Sallam

Summary

This article provides a comprehensive review of the fundamental concepts of Object-Oriented Programming (OOP) in Python, covering classes, objects, attributes, methods, properties, abstraction, encapsulation, and copying mechanisms.

Abstract

The article serves as the concluding part of a complete course in Object-Oriented Programming (OOP) using Python. It revisits the core principles of OOP, including the definition of classes and objects, the distinction between instance and class attributes, and the various types of methods such as instance, class, and static methods. It also discusses the concept of properties for attribute manipulation, the importance of abstraction and encapsulation in OOP, and the differences between shallow and deep copying. The author emphasizes the practical applications of these concepts through examples and encourages readers to subscribe for future content and consider Medium membership for unlimited access to stories and earning potential.

Opinions

  • The author expresses congratulations to the reader for reaching the conclusion of the OOP basics course, indicating a sense of accomplishment for the reader's progress.
  • The article is presented as part of a larger series, suggesting a structured and comprehensive approach to learning Python OOP.
  • The use of figures and real-world examples, such as a "Bank Account" class, is intended to aid in the understanding of complex OOP concepts.
  • The author advocates for the importance of OOP principles like encapsulation and abstraction in creating maintainable and shareable code.
  • A call to action is included for readers to subscribe to the author's posts and follow them on Medium, indicating the value the author places on reader engagement and support.
  • The author highlights the potential financial benefits for writers on Medium, suggesting an opportunity for readers to both learn and earn through the platform.

OOP Basics Conclusion: Python OOP Complete Course — Part 10

A Quick Review of the Basic Concepts in OOP.

Photo by Ann H on Pexles

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

If you reach this article, Congratulations!

You have done a super job so far, and now you have all the basics that you need to create classes and objects in Python. Now, let us review all the important concepts that have been covered till now.

This article will cover the following outlines:

  1. What Is OOP?
  2. Attributes Types
  3. Methods Types
  4. What Is Property?
  5. Abstraction and Encapsulation
  6. Copy in OOP

1. What Is OOP?

OOP is a programming style in which developers can create any complex data type by binding together two things:

  • Data Properties (attributes) that describe the new type.
  • Functions (methods) that process these attributes.

In OOP context:

  • The newly created data type is called a class.
  • The data properties associated with this class are called attributes.
  • The functions that handle these attributes are called methods.

As it has been explained in the previous articles, “Bank Account” class:

  1. Have some attributes, like the account holder's name, account ID, and the balance in this bank account.
  2. These attributes could be processed by the methods withdraw() and deposit(). Refer to Figure 1.
Figure 1: Define Bank Account (Image By Author).

When you have a new class, a new blueprint or a prototype, you can create as many objects from this class.

So, from this Bank Account, you can define an instance or an object account1 and specify real values for the attributes of that object. Refer to Figure 2.

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

Once you have an object from your class, the methods of that class could be used to handle and process the attributes of that object.

2. Attributes Types

There are two types of attributes as follows:

Instance Attributes

  • It belongs to the class instances.
  • Each class instance (object) has its values of these attributes, which means these attributes are not common and are not shared between the class objects.

Class Attributes

  • It belongs to the class itself.
  • These attributes are shared between all the class objects, which means if one object tries to update the value of this attribute, it will be changed for all the other attributes. Refer to Figure 3.
Figure 3: Instance Attributes vs Class Attributes (Image By Author).

3. Methods Types

There are three types of methods as follows:

Instance Method

  • The first parameter is (self) and this parameter represents the object that is calling this method, (self) is automatically passed as the first argument in the method.
  • To define this method, you don’t use any decorator.
  • This method is used to handle and process the instance state or in other words instance attributes.

Class Method

  • The first parameter is (cls), which is a reference to the class and is automatically passed as the first parameter.
  • This method is marked with @classmethod decorator.
  • This method is used to handle and modify the class state, or in other words, class attributes.

Static Method

  • It doesn’t have access either to the object or to the class, and they just behave like a regular function.
  • This method is marked with @staticmethod decorator.
  • They cannot modify neither class nor object state. Refer to Figure 4.
Figure 4: Instance Methods vs. Class Methods vs. Static Methods (Image By Author).

After that, we learned about the property.

4. What Is Property?

Sometimes you need a way to do a kind of preprocessing (logic) when you want to get, set or delete an attribute.

Therefore, you define a Property by using:

  • property function(), which is a built-in function that creates and returns a property object.
  • property decorator.

Then you have learned about the first two pillars of OOP which are Abstraction and Encapsulation.

5. Abstraction and Encapsulation

  • Encapsulation is combining all the attributes and the methods that the class needs under one umbrella (class). It is a very important pillar of OOP because it makes your code easy to maintain.
  • Abstraction is hiding away the implementation details from the external user. OOP satisfies this concept because by using it you expose to the external user the methods that should be used to benefit from the core functionality of your class. This pillar makes your code easy to be shared with others because they will not be aware of the complex details of that class.

The last concept that has been covered is Copy in OOP.

6. Copy in OOP

  • Shallow copy: create a reference to the same location in the memory. So, If you assign one object to another that doesn’t mean you are creating a standalone object, but a shallow copy of this object. Any changes to the original object will affect the copy.
  • If you want to create a standalone copy, you have to create a deep copy. The function deepcopy() from the copy module is used to create a deep copy. Any changes to the original object will not affect the copy.
  • An identity operator is used to check if one object is a shallow copy of another object or not.

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 9:Shallow Copy and Deep Copy

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

Part 11:Special Methods Overview

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

An easy guide

10 min read