Saturday, 10 October 2026

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

 


Code Explanation:

Step 1: Define the Base Class

class Base:

This creates a parent class named Base. Other classes can inherit its attributes and methods.

Step 2: Create a Class Attribute
x = 1

The class attribute x is initialized to 1.
Initially:
Base.x = 1


Step 3: Define __init_subclass__()
def __init_subclass__(cls):

Python automatically calls __init_subclass__() when a new class inherits from Base or from a subclass that inherits this method.
The parameter cls refers to the newly created subclass.
For example, when class A is created:
cls → A

Step 4: Update the Subclass Attribute
cls.x += 2

This is equivalent to:
cls.x = cls.x + 2

Python looks up the current value of cls.x, adds 2, and assigns the result to the subclass.
If the subclass does not have its own x, Python initially finds the inherited value.

Step 5: Create Class A
class A(Base):    pass

Class A inherits from Base.
When A is created, Python automatically calls:
Base.__init_subclass__(A)


Initially, A inherits Base.x, which is 1.
So:
A.x = 1 + 2
A.x = 3


The augmented assignment creates an x attribute directly on A. It does not change Base.x.
Now:
Base.x = 1
A.x    = 3


Step 6: Create Class B
class B(A):    pass

Class B inherits from A.
The inherited __init_subclass__() method runs again, this time with cls referring to B.
Since B does not initially define its own x, Python finds A.x, which is 3.
Therefore:
B.x = 3 + 2
B.x = 5

Now each class has its own effective value:
Base.x = 1
A.x    = 3
B.x    = 5

Step 7: Print the Values
print(Base.x, A.x, B.x)

Python prints the three class attributes in order:
- Base.x → 1
- A.x → 3
- B.x → 5

Final output:

1 3 5


300 Days Python Coding Challenges with Explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (347) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (435) Data Strucures (19) Deep Learning (222) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (78) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (407) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1380) Python Coding Challenge (1272) Python Library (22) Python Mathematics (20) Python Mistakes (51) Python Pattern Challenge (19) Python Quiz (655) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)