OOP Miras | Inheritance in Python | Multi-Level Inheritance | Multiple Inheritance | MRO #kody_az
Dərs haqqında
Python-da Miras (Inheritance) nədir?
Miras — obyekt yönlü proqramlaşdırmada (OOP) bir sinifin (uşaq/törəmə sinifin) başqa bir sinifdən (valideyn/super sinifdən) xüsusiyyətləri və metodları əldə etməsinə imkan verən bir mexanizmdir.
Bu, kodun təkrar istifadəsini, modulluğu və məntiqi iyerarxiyanı təmin edir.
Python-da müxtəlif miras növləri var:
• Tək miras (Single Inheritance)
• Çoxsəviyyəli miras (Multi-Level Inheritance)
• Çoxlu miras (Multiple Inheritance)
• Hierarxik miras (Hierarchical Inheritance)
• Hibrid miras (Hybrid Inheritance)
Gəlin, əsas növlərə nəzər salaq.
⸻
Çoxsəviyyəli Miras (Multi-Level Inheritance)
Bu halda, bir sinif başqa bir törəmə sinifdən, o sinif isə başqa bir əsas sinifdən miras alır. Yəni, zəncirvari bir miras qurulur.
• 1-ci səviyyə: Əsas sinif (baza davranışlarını təyin edir)
• 2-ci səviyyə: Ondan miras alan sinif (öz əlavələrini edir)
• 3-cü səviyyə: Daha da ixtisaslaşmış törəmə sinif
Bu, siniflər arasında dərin əlaqələr qurmağa imkan verir, lakin böyük strukturlarda izləməsi çətin ola bilər.
⸻
Çoxlu Miras (Multiple Inheritance)
Çoxlu miras zamanı bir sinif birdən çox valideyn sinifdən miras alır. Bu sinif, hər iki (və ya daha çox) valideynin metod və atributlarını əldə edə bilir.
Bu üsul:
• Çox çevikdir, çünki fərqli funksiyaları birləşdirə bilir.
• Risklidir, çünki eyni adlı metodlar və atributlar olduqda qarışıqlıq yarana bilər.
Python bunu idarə etmək üçün MRO adlı bir mexanizmdən istifadə edir.
⸻
MRO – Metodların Axtarış Qaydası (Method Resolution Order)
MRO (Method Resolution Order) — Python-un siniflər iyerarxiyasında bir metodun və ya atributun axtarış sırasını müəyyən etdiyi qaydadır.
Python C3 Linearization adlı alqoritmdən istifadə edir. Bu alqoritm:
• Soldan sağa və dərinlik üzrə axtarış edir,
• Təkrar sinifləri təkcə bir dəfə əlavə edir,
• Ən stabil və məntiqli sıralama qaydasını verir.
MRO-nun məqsədi, çoxlu miras zamanı Python-un hansı sinifdə metod və ya atribut axtaracağını əvvəlcədən bilməkdir.
=======
Inheritance is a mechanism in object-oriented programming that allows a class (child) to inherit the attributes and behaviors (methods) of another class (parent). This promotes code reuse, modularity, and logical hierarchy.
There are several types of inheritance in Python, including:
• Single Inheritance
• Multi-Level Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Let’s explore a few in more depth.
⸻
Multi-Level Inheritance
In multi-level inheritance, a class is derived from a class that is already derived from another class. Think of it as a chain of inheritance.
• Level 1: Base class defines general behaviors.
• Level 2: A subclass inherits from the base and adds more features.
• Level 3: A subclass of that subclass further specializes or modifies behavior.
This allows complex hierarchies and fine-grained specialization, but it can also make the structure harder to trace and debug as it grows.
⸻
Multiple Inheritance
Multiple inheritance occurs when a class inherits from more than one parent class. This means the child class can access members from all parent classes.
This is powerful but potentially problematic because:
• If multiple parents have methods or properties with the same name, Python must decide which one to use.
• This is where the MRO comes into play (explained below).
Python supports multiple inheritance using a well-defined strategy, but it’s recommended to use it cautiously to avoid confusion and conflicts.
⸻
MRO (Method Resolution Order)
MRO stands for Method Resolution Order. It is the order in which Python looks for a method or attribute in the hierarchy of classes during execution.
Python uses the C3 Linearization algorithm for MRO. It works left to right, depth-first, and avoids repeating parent classes. It guarantees a consistent and predictable order when multiple inheritance is involved.
You can view the MRO of a class using the built-in method resolution tools (mro() or __mro__), but the key idea is:
• Python searches from the current class upward through the chain.
• If a method or attribute is found earlier in the MRO, it’s used and stops looking.
#python #kody_az #kodyaz #viral #fleet #jetbrains #pycharm #oop #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #azərbaycan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz