class Parent:
def __init__(self):
self.value = "Test"
print("Parent class __init()__ mathod is called")
def test(self):
print("Parent class of test() mathod")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child class __init()__ mathod is called")
child = Child()
child.test()
print(child.value)
### Class Definitions and Inheritance
- **`class Parent:`** Defines a base (or parent) class named `Parent`. It includes:
- An `__init__` method that initializes a new instance of the `Parent` class, setting the attribute `value` to the string `"Test"` and printing a message indicating that the `Parent` class's `__init__` method has been called.
- A `test` method that prints a message when called.
- **`class Child(Parent):`** Defines a derived (or child) class named `Child` that inherits from `Parent`. It includes:
- An `__init__` method that first calls `super().__init__()` to ensure that the `Parent` class's `__init__` method is called. This is why the attribute `value` and the method `test` are accessible from instances of `Child`. After calling the parent's `__init__`, it prints a message indicating that the `Child` class's `__init__` method has been called.
### Object Instantiation and Method Calls
- **`child = Child()`**: Creates an instance of the `Child` class. This automatically calls the `Child` class's `__init__` method, which in turn calls the `Parent` class's `__init__` method. The output from this sequence of method calls is:
- "Parent class __init()__ method is called"
- "Child class __init()__ method is called"
- **`child.test()`**: Calls the `test` method on the `child` instance. Since `Child` does not override `test`, the method is inherited from `Parent`, resulting in the output:
- "Parent class of test() method"
- **`print(child.value)`**: Accesses the `value` attribute of the `child` instance. This attribute was set in the `Parent` class's `__init__` method to the string `"Test"`, and since `Child` inherits from `Parent`, `child` has this attribute. The output is:
- "Test"
### Key Concepts Illustrated
- **Inheritance**: `Child` inherits from `Parent`, meaning it receives all of the methods and attributes of the `Parent` class.
- **`super()` Function**: Used to call methods of the parent class from the child class, including the `__init__` method to ensure proper initialization.
- **Method Resolution Order (MRO)**: Python looks up methods first in the child class before looking in the parent class. Since `Child` does not define a `test` method, the `test` method of `Parent` is called.
This example effectively demonstrates how inheritance allows for code reuse and how child classes can extend or modify the behavior of parent classes.
'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글
python) Understanding Custom Exception Handling in Python (0) | 2024.11.02 |
---|---|
python) custom exception class (2) | 2024.11.01 |
python) Put only things that can be converted to numbers into a list (2) | 2024.10.30 |
python) Overriding (1) | 2024.10.29 |
python) size comparison function (2) | 2024.10.28 |