Tuesday, 15 September 2026

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

 



Code Explanation:

1. Define Class D
class D:

A class named D is created.

This class is going to behave as a descriptor because it defines the special method __get__().

A descriptor is an object that can control what happens when an attribute is accessed.

2. Define __set_name__()
def __set_name__(self, owner, name):

__set_name__() is automatically called by Python when a descriptor is assigned to a class attribute during class creation.

It receives three important values:

self → the descriptor object
owner → the class containing the descriptor
name → the name of the attribute

Later, when Python creates class A, this will effectively become:

__set_name__(A, "value")

So:

owner → A
name  → "value"

3. Store the Attribute Name
self.name = name

The value of name is:

"value"

Therefore:

self.name = "value"

The descriptor now remembers the name under which it was assigned.

Conceptually:

D object
   ↓
name = "value"

4. Define __get__()
def __get__(self, obj, owner):

__get__() controls what Python returns when the descriptor is accessed.

The parameters are:

self → descriptor object
obj → instance through which the attribute is accessed
owner → class that owns the descriptor

In this question, we access:

A.value

through the class, not through an instance.

Therefore:

obj → None
owner → A

5. Build the Return Value
return f"{owner.__name__}:{self.name}"

This is the most important line.

Let's break it into two parts.

owner.__name__

Here:

owner → A

Therefore:

owner.__name__ → "A"
self.name

Earlier, we stored:

self.name → "value"

So the f-string becomes:

"A:value"

The __get__() method therefore returns:

A:value

6. Create Class A
class A:
    value = D()

First:

D()

creates a descriptor object.

That object is assigned to:

A.value

During class creation, Python automatically calls:

D.__set_name__(A, "value")

Therefore:

self.name = "value"

7. Access A.value
print(A.value)

This is where the descriptor mechanism is triggered.

Python sees that value is a descriptor because the D object has a __get__() method.

So instead of simply returning the D object, Python calls:

D.__get__(descriptor, None, A)

Therefore:

obj   = None
owner = A

8. Execute __get__()

Inside __get__():

return f"{owner.__name__}:{self.name}"

we have:

owner.__name__ → "A"
self.name      → "value"

Therefore:

"A" + ":" + "value"

produces:

"A:value"

9. print() Displays the Result
print(A.value)

The descriptor returns:

A:value

So Python prints:

A:value

Book: Python Functions in Depth — Writing Clean, Reusable, and Powerful Code

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

 


Code Explanation:

1. Define Class A
class A:

A class named A is created.

The class contains two special methods:

__new__() → creates and returns the object
__init__() → initializes the already-created object

The important thing is that __new__() runs before __init__().

2. Define __new__()
def __new__(cls):

__new__() is responsible for creating the instance.

Here, cls refers to the class being instantiated:

cls → A

When we later write:

a = A()

Python first calls:

A.__new__(A)

3. Create the Object
obj = super().__new__(cls)

This calls the parent implementation of __new__().

Since A ultimately inherits from object, this effectively calls:

object.__new__(A)

A new instance of A is created.

Conceptually:

object.__new__(A)
        ↓
new A object
        ↓
obj

At this point, obj is an actual A instance.

4. Assign x = 5
obj.x = 5

The newly created object gets an attribute named x.

So:

obj.x = 5

Current state:

obj
 └── x = 5

5. Return the Object
return obj

__new__() returns the newly created object.

This returned object becomes the object that Python will initialize next.

So conceptually:

__new__()
   ↓
returns obj
   ↓
obj becomes self
   ↓
__init__()

6. Define __init__()
def __init__(self):

__init__() is called after __new__() successfully returns an instance of the class.

Here, self refers to the exact object returned by __new__().

Therefore:

self = obj

And we already know:

self.x = 5

7. Increase x
self.x += 10

This is equivalent to:

self.x = self.x + 10

Current value:

self.x = 5

Therefore:

5 + 10 = 15

Now:

self.x = 15

8. Create Object a
a = A()

This triggers the complete sequence:

A()
 ↓
__new__()
 ↓
create object
 ↓
obj.x = 5
 ↓
return obj
 ↓
__init__()
 ↓
self.x += 10
 ↓
x = 15

Therefore:

a.x = 15

9. Print a.x
print(a.x)

a.x is now:

15

So the final output is:

15

Books: 1000 Days Python Coding Challenges with Explanation

Monday, 14 September 2026

Wish Happy Ganesh Chaturthi in Python (Turtle)




 from turtle import *

import turtle as tur import random import time t=tur.Turtle() tur.title("clcoding.com") tur.speed(8) tur.bgcolor("black") tur.color("red") tur.pensize(6) tur.left(60) tur.fd(50) tur.left(15) tur.circle(100,90) tur.fd(30) tur .pensize(10) tur.penup() tur.right(90) tur.fd(20) tur.pendown() tur.right(40) tur.circle(-50,90) tur.fd(20) tur.left(150) #seconde head curve tur.color("red") tur.penup() tur.fd(40) tur.left(20) tur.pendown() tur.circle(50,90) #third head curve #goto beginning tur.color("red") tur.penup() goto(0,0) tur.pensize(5) tur.pendown() tur.left(30) tur.fd(120) tur.circle(60,270) #eyes tur.color("silver") tur.penup() tur.forward(30) tur.right(50) tur.forward(135) tur.pendown() tur.pensize(8) tur.circle(50,90) tur.left(95) tur.penup() tur.circle(60,75) #eyebrows tur.penup() tur.forward(15) tur.left(90) tur.pensize(2) tur.pendown() tur.circle(70,90) #ears tur.pensize(5) tur.penup() tur.forward(75) tur.right(90) tur.forward(20) tur.pendown() tur.circle(90,90) tur.forward(20) tur.circle(30,170) tur.right(180) tur.circle(28,180) tur.right(160) tur.circle(25,180) tur.right(160) tur.circle(22,160) tur.forward(20) tur.circle(60,45) #trunk tur.penup() goto(0,0) tur.left(130) tur.fd(140) tur.right(250) tur.backward(20) tur.circle(80,20) tur.circle(20,40) tur.right(110) tur.penup() tur.fd(20) tur.pendown() tur.pensize(10) tur.forward(50) tur.circle(100,80) tur.pensize(9) tur.circle(150,50) tur.pensize(7) tur.circle(100,60) tur.pensize(5) tur.circle(90,60) tur.pensize(4) tur.circle(40,60) tur.circle(10,90) #head tur.color("red") tur.penup() goto(0,0) goto(-90,290) tur.right(230) tur.pendown() tur.circle(-100,50) tur.circle(200,20) tur.circle(50,30) tur.right(180) tur.circle(50,30) tur.circle(200,20) tur.circle(-100,40) tur.right(95) tur.penup() tur.fd(40) tur.right(90) tur.pendown() tur.circle(100,40) tur.penup() tur.circle(35,120) tur.right(30) tur.pendown() tur.pensize(1) tur.circle(60,50) #done tur.penup() goto(-70,90) tur.fillcolor("red") tur.begin_fill() tur.circle(20,180) tur.end_fill() tur.penup() tur.left(75) tur.fillcolor("red") tur.begin_fill() tur.circle(70,35) tur.end_fill() tur.left(180) tur.backward(10) tur.pendown() tur.left(6) tur.pensize(5) tur.color("red") tur.circle(-80,40) tur.penup() goto(0,0) #borderrrr tur.write(" Happy Ganesh Chaturthi",font=("arial",22,"normal"),align="left") tur.penup() goto(0,420) tur.right(90) done()

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (343) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (431) Data Strucures (18) Deep Learning (220) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) 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 (403) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1371) Python Coding Challenge (1239) Python Library (1) Python Mathematics (17) Python Mistakes (51) Python Pattern Challenge (4) Python Quiz (630) 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)