CodeX
Published in

CodeX

Introduction to OOP in Python

Class Methods, Private Attributes, Information Hiding, Encapsulation and Singleton Design Pattern

Hi. I am going to write about Python OOP 101. Class method types, some OOP principles such as information hiding and encapsulation etc., and what Singleton is which is a design pattern are the topics that you can find in this text.

Class Method Types

We have 3 types of class methods.

  1. Object Methods: We can use these methods from instantiated objects.
  2. Class Methods: We can use these methods from the classes directly.
  3. Static Methods: We can use these methods from both direct classes and instantiated objects.

Actually, I want to point out this right here. Methods are functions that are defined under the classes. However, we call them “method” when we defined them under a class instead of “function”.

Prepare A Class and Constructor Method

There are some special methods. The constructor method is one of them. When an object is instantiated this method assigns the object’s attributes. To use this method, we use __init__ function in Python.

Object Methods

We can use these methods from the instantiated objects. The syntax is object.method_name([args]). In Python, we have to give self as a first parameter.

Class Methods

These methods take the classes (cls) as first parameters instead of self. The syntax is CLASS_NAME.method([args]). They use classmethod decorator.

Static Methods

We can use these methods from both direct classes and instantiated objects. The methods use staticmethod decorator. The parameters are important for these methods. Because these methods don’t represent the objects or classes themselves.

Information Hiding & Encapsulation

Information hiding and encapsulation are principles of object-oriented programming. By using these principles we can hide our classes’ or objects’ attributes to prevent changing them.

Private Attributes & Methods

Actually, if you read the 2 texts below before continuing, it would be better to understand why we use “__” etc.

I am going to add “__” prefix to attributes that I want to make private.

In this example, we hide instance_count attribute to restrict its accessibility.

Design Patterns

Design Patterns are permanent solutions to recurring design problems. These patterns are solutions that software developers have developed and agreed on to permanently solve the problems they have been working on for many years. By using design patterns, we can more easily solve problems that have already been determined/solved. There are 3 types of design patterns.

  • Creational Patterns
  • Structural Patterns
  • Behavioural Patterns

In this text, I will show an applied example of the Singleton pattern.

Singleton Pattern

Singleton is a “creational pattern”. Although it is one of the simplest examples of design patterns, it is a very powerful technique. In short, it is to restrict a class to have only one instantiated object.

Finally

Hopefully, you enjoyed it. I wish you happy coding.

Regards

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Baysan

Baysan

321 Followers

Lifelong learner & Freelancer. I use technology that helps me. I’m currently working as a Business Intelligence & Backend Developer. mebaysan.com