Sunday, 4 October 2026

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

 

Explanation:

1. ๐Ÿ—️ Define the Class
class A:


This creates a class named A.
We will later create an object from this class.

2. ๐Ÿ“Œ Create a Class Attribute
x = 10


Here, x is a class attribute.
Every object of A can access it unless an instance attribute with the same name overrides it.
So:
a.x


can find the value:
10

3. ๐Ÿ” Define __getattr__()
def __getattr__(self, name):


__getattr__() is a special method that Python calls when an attribute cannot be found normally.
The name parameter receives the name of the missing attribute.
For example:
a.y


doesn't find y, so Python effectively calls:
a.__getattr__("y")

4. ๐ŸŽฏ Return a Default Value
return 99

Whenever an attribute is missing, this method returns 99.
So:
a.y


becomes:
99

Notice that __getattr__() does not run for attributes that already exist.

5. ๐Ÿ†• Create an Object
a = A()


An object a is created from class A.
At this point, a can access the class attribute:
a.x → 10

6. ๐Ÿ–จ️ Access a.x
print(a.x, a.y)


First Python evaluates:
a.x

x exists in class A.
Therefore, Python gets:
a.x → 10

__getattr__() is not called.

7. ⚠️ Access a.y
Python then evaluates:
a.y


There is no y attribute in the instance or class.
So Python calls:
__getattr__(a, "y")


The method returns:
99

Therefore:
a.y → 99

๐Ÿ”„ Internal Flow
a.x
 ↓
Found normally
 ↓
10

But:
a.y
 ↓
Not found
 ↓
__getattr__("y")
 ↓
99

๐Ÿง  __getattr__() vs __getattribute__()
Method When called?
__getattribute__() For every attribute access
__getattr__() Only when normal lookup fails


๐Ÿ’ก Interview Trick
The biggest point to remember:
__getattr__() is a fallback mechanism for missing attributes.

So the final result is:

✅ Output
10 99

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (346) 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) 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 (1377) Python Coding Challenge (1267) Python Library (19) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (16) Python Quiz (650) 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)