Wednesday, 25 November 2026

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

 


Code Explanation:

1. Define the Double Class

class Double:

- Creates a class named Double.

- This class acts as a descriptor.

- A descriptor controls how an attribute behaves when accessed.


2. Define the __get__() Method

def __get__(self, obj, owner):

- __get__() is a special method used by descriptors.

- Python calls it automatically when we access the doubled attribute.

- self refers to the descriptor object.

- obj refers to the object accessing the attribute.

- owner refers to the class that owns the descriptor.


3. Return Double the Value

return obj._value * 2

- Gets the _value stored in the object.

- Multiplies it by 2.

- Returns the calculated result.

For this example:

7 * 2 = 14


4. Create the Demo Class

class Demo:

- Defines a class named Demo.

- This class uses the Double descriptor.


5. Create the Descriptor Attribute

doubled = Double()

- Creates an instance of the Double class.

- Assigns it to the doubled attribute.

- Now doubled is controlled by the descriptor.


6. Initialize the Object

def __init__(self, x):    self._value = x

- The constructor runs when an object is created.

- x receives the value passed during object creation.

- Stores that value in the _value instance variable.


7. Create an Object

d = Demo(7)

- Creates an object of the Demo class.

- Passes 7 to the constructor.

- Therefore, d._value becomes 7.


8. Access the Descriptor

print(d.doubled)

- Accesses the doubled attribute.

- Python automatically calls the descriptor's __get__() method.

- The method calculates 7 * 2.

- The returned value is printed.


Final Output

14


Book: 300 Days Python Coding Challenges with Explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (363) 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 (32) data management (16) Data Science (449) Data Strucures (22) Deep Learning (229) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (2) 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 (421) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (36) Python (1415) Python Bug (1) Python Coding Challenge (1319) Python Library (29) Python Mathematics (25) Python Mistakes (51) Python Pattern Challenge (48) Python Quiz (699) Python Tips (115) Python Turtle (3) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (57) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)