Tuesday, 10 November 2026

Python Coding challenge - Day 1307| What is the output of the following Python Code?

 


Code Explanation:

1. Creating the First Parent Class
class A:

- Defines a class named A.
- This class acts as the base parent class.
- Other classes will inherit its methods.

2. Defining the Show Method
    def show(self):        return "A"

- Defines an instance method named show().
- The method returns the string "A".
- This method will eventually be reached through the inheritance chain.

3. Creating the Second Class
class B(A):
- Defines a class named B.
- B inherits from class A.
- Therefore, class B can access methods defined in class A.

4. Overriding the Parent Method
    def show(self):        return "B" + super().show()

- Defines another show() method inside class B.
- This overrides the inherited show() method.
- super().show() calls the next implementation of show() according to Python's MRO.
- In this case, when used through class B directly, the next class is A.
- The returned strings are combined.

5. Creating the Third Class
class C(A):

- Defines another class named C.
- It also inherits from class A.
- Both B and C share the same parent class.

6. Overriding the Show Method Again
    def show(self):        return "C" + super().show()

- Defines a show() method inside class C.
- It overrides the inherited method.
- "C" is concatenated with the result of super().show().
- The next method is determined by the MRO of the actual object.

7. Creating the Multiple Inheritance Class
class D(B, C):    pass

- Defines class D.
- It inherits from both B and C.
- This is called Multiple Inheritance.
- pass means no additional methods or attributes are defined inside class D.

8. Calling the Show Method
print(D().show())
- D() creates an object of class D.
- .show() calls the method according to Python's Method Resolution Order.
- Python follows the MRO sequence:
D → B → C → A → object

- Class B adds "B" to the result.
- super() inside B moves to C.
- Class C adds "C" to the result.
- super() inside C moves to A.
- Class A returns "A".

Understanding MRO Execution
Method Resolution Order
Class D
Child class

Class B
B
Class C
C

Class A
A
Returns the final string
Actual execution order: D → B → C → A

Final Output
Correct Output
BCA

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (359) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (20) C (78) C# (12) C++ (83) cloud (1) Course (97) Coursera (307) Cybersecurity (37) data (10) Data Analysis (48) Data Analytics (31) data management (16) Data Science (443) Data Strucures (21) Deep Learning (228) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (80) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (417) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (36) Python (1405) Python Bug (1) Python Coding Challenge (1304) Python Library (29) Python Mathematics (24) Python Mistakes (51) Python Pattern Challenge (38) Python Quiz (685) Python Tips (115) Python Turtle (1) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (56) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)