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.
Python OOP Metodları
Merhabalar, bu yazımda Python’daki metod türlerinden bahsetmeye çalışacağım.
Class Method Types
We have 3 types of class methods.
- Object Methods: We can use these methods from instantiated objects.
- Class Methods: We can use these methods from the classes directly.
- 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.
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.
These methods take the classes (cls) as first parameters instead of self. The syntax is
CLASS_NAME.method([args]). They use
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.
Underscore (_) in Python - GeeksforGeeks
In this article, we are going to see Underscore (_) in Python. Following are different places where "_" is used in…
Private Variables in Python - GeeksforGeeks
Prerequisite: Underscore in Python In Python, there is no existence of "Private" instance variables that cannot be…
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 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 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.
Hopefully, you enjoyed it. I wish you happy coding.