Wednesday, 11 March 2026

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

 


Code Explanation:

1. Defining Class D
class D:

Explanation:

This line creates a class named D.

This class will act as a descriptor.

A descriptor is a class that defines special methods like __get__, __set__, or __delete__ to control attribute access.

2. Defining the __get__ Method
def __get__(self, obj, objtype):

Explanation:

__get__ is a descriptor method.

It is automatically called when the attribute is accessed (read).

Parameters:

self → descriptor object (D)

obj → instance of class A

objtype → the class (A)

When we access a.x, Python internally calls:

D.__get__(descriptor, a, A)
3. Returning a Value
return 50

Explanation:

Whenever x is accessed through an object, this method returns 50.

So the descriptor controls the value returned.

Meaning:

a.x → 50
4. Defining Class A
class A:

Explanation:

This creates another class named A.

5. Creating Descriptor Attribute
x = D()

Explanation:

Here an object of class D is assigned to attribute x.

This makes x a descriptor attribute.

Accessing x will trigger the __get__ method.

So:

A.x → descriptor object of class D
6. Creating an Object
a = A()

Explanation:

This creates an instance a of class A.

7. Adding Attribute Directly to Object Dictionary
a.__dict__['x'] = 20

Explanation:

__dict__ stores all instance attributes of an object.

This line manually adds an attribute:

x = 20

inside the object's dictionary.

So internally:

a.__dict__ = {'x': 20}

8. Printing a.x
print(a.x)

Explanation:

Python checks attributes in this order:

Data descriptor (__get__ + __set__)

Instance dictionary

Non-data descriptor (__get__ only)

Class attributes

Here:

D defines only __get__, so it is a non-data descriptor.

Python first checks instance dictionary.

It finds:

a.__dict__['x'] = 20

So Python returns 20 instead of calling the descriptor.

9. Final Output
20

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (216) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (9) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (86) Coursera (300) Cybersecurity (29) data (4) Data Analysis (27) Data Analytics (20) data management (15) Data Science (320) Data Strucures (16) Deep Learning (131) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (3) flutter (1) FPL (17) Generative AI (65) Git (10) Google (50) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (259) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1263) Python Coding Challenge (1072) Python Mistakes (50) Python Quiz (439) 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)