1. Inheritance
Inheritance in Python is a mechanism that allows one class to inherit the properties and methods of another class. This enables code reuse and extensibility. By default, all classes in Python inherit from a base class called object.
- Base Class (or Parent Class): The class from which inheritance is derived.
- Derived Class (or Child Class): The class that inherits properties and methods through inheritance.
2. Order of Constructor Calls in Class Inheritance
In Python, inheritance refers to the ability to pass properties and methods from one class to another class. Inheritance allows user to reuse and extend existing code. Basically, every class in Python inherits from a base class called object.
- Child class constructor is called
- The parent class constructor is called from the child class constructor
- If the child constructor directly calls the parent constructor, the super() function is used.
- Once the parent class constructor finishes executing, it returns to the child class constructor, and the child class constructor code is executed.
3. The object class
object is the base class of all classes in Python. The object class defines the basic behavior and characteristics of all objects in Python.
- All classes inherit from object: Everything in Python is an object. Numbers, strings, lists, custom objects, etc. are all objects.
- Methods provided by object: The object class provides several basic methods.
4. Method Overriding
Method overriding is a key concept in object-oriented programming where a subclass (child class) redefines a method of the superclass (parent class). Overriding allows you to change or extend the behavior of an inherited method in the subclass.
5. Multiple Inheritance
Multiple inheritance is the ability of a class to inherit from two or more parent classes. Python supports multiple inheritance, unlike many other object-oriented languages. Multiple inheritance can improve code reusability but can also increase complexity, so it should be used with caution.
C3 Linearization Algorithm
- An algorithm used to calculate the Method Resolution Order (MRO) in Python's multiple inheritance.
- Designed to clearly determine the order of method calls in complex inheritance structures
6. The super() Method
super() is a built-in function in Python used for tasks related to inheritance. It is primarily used to call methods of the parent class from the child class. The main purpose of super() is to override a parent class method in a child class while still calling the original method of the parent class within the overridden method.
6-1. Basic Usage
6-2. Using the __init__() Method
6-3. Using in Multiple Inheritance
'Python(Eng. ver)' 카테고리의 다른 글
18. Python Error Handling (0) | 2024.03.19 |
---|---|
17. Special Method (0) | 2024.03.19 |
15. Object-oriented programming and Class (0) | 2024.03.18 |
14. Callbacks and Lambdas in Python (0) | 2024.03.18 |
13. Scope of Variable in Python (0) | 2024.03.18 |