Sunday, 22 February 2026

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

 


Code Explanation:

๐Ÿ”น 1. Defining the Descriptor Class
class D:

Creates a class D

This class will act as a descriptor

๐Ÿ”น 2. Implementing __get__
def __get__(self, obj, objtype):
    return 99

__get__ makes D a descriptor

It controls how an attribute is read

Always returns 99

Parameters:

self → descriptor object

obj → instance accessing the attribute (a)

objtype → owner class (A)

๐Ÿ“Œ Important:
This descriptor defines only __get__, so it is a non-data descriptor.

๐Ÿ”น 3. Defining Class A
class A:

Creates a normal class A

๐Ÿ”น 4. Assigning Descriptor to Class Attribute
x = D()

x is a class attribute

Value is an instance of D

Since D has __get__, x is managed by the descriptor

๐Ÿ”น 5. Creating an Instance of A
a = A()

Creates object a

At this moment:

a.__dict__ is empty

x exists only in the class

๐Ÿ”น 6. Assigning to a.x
a.x = 5
What happens internally:

Python does NOT call the descriptor

Because D has no __set__

Python creates an instance attribute

a.__dict__['x'] = 5

๐Ÿ“Œ This overrides the descriptor for this instance.

๐Ÿ”น 7. Accessing a.x
print(a.x)
Attribute lookup order:

Instance dictionary (a.__dict__) → ✅ finds x = 5

Descriptor is skipped

Returned value is 5

✅ Final Output
5

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (206) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (8) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (29) data (1) Data Analysis (26) Data Analytics (20) data management (15) Data Science (297) Data Strucures (16) Deep Learning (122) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (61) Git (9) Google (48) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (247) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1257) Python Coding Challenge (1042) Python Mistakes (50) Python Quiz (427) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)