Showing posts with label Python Coding Challenge. Show all posts
Showing posts with label Python Coding Challenge. Show all posts

Wednesday, 18 February 2026

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

 


Code Explanation:

1. Importing ABC Tools
from abc import ABC, abstractmethod

Imports:

ABC → Base class for creating abstract classes

abstractmethod → Decorator to mark methods as mandatory to override

๐Ÿ“Œ Used to enforce method implementation in subclasses.

๐Ÿ”น 2. Defining an Abstract Base Class A
class A(ABC):


A inherits from ABC

This makes A an abstract class

Abstract classes cannot be instantiated directly

๐Ÿ”น 3. Declaring an Abstract Method
@abstractmethod
def f(self): pass

Declares f() as an abstract method

Any concrete subclass must provide an implementation

pass means no implementation in A

๐Ÿ“Œ If a subclass doesn’t implement f, it cannot be instantiated

๐Ÿ”น 4. Defining Subclass B
class B(A):

B inherits from abstract class A

Python checks whether B implements all abstract methods

๐Ÿ”น 5. Implementing the Abstract Method (Tricky Part)
f = lambda self: 10

Assigns a function object to f

This counts as implementing the abstract method

Equivalent to:

def f(self):
    return 10


๐Ÿ“Œ Python does not care about syntax, only that f exists and is callable.

๐Ÿ”น 6. Creating an Object of B
B()


Allowed because:

All abstract methods are implemented

B is now a concrete class

๐Ÿ”น 7. Calling the Method
print(B().f())


Step-by-step:

B() → creates an instance of B

.f() → calls the lambda method

Lambda returns 10

print() prints the value

✅ Final Output
10

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

 


Code Explanation:

1. Defining Class A
class A:
    x = "A"

Creates a base class A

Defines a class variable x with value "A"

Any subclass can inherit this variable unless it overrides it

๐Ÿ”น 2. Defining Class B (Inherits from A)
class B(A):
    x = "B"

B inherits from A

x is redefined in B

This overrides A.x for class B and its subclasses

๐Ÿ“Œ Now:

A.x → "A"

B.x → "B"

๐Ÿ”น 3. Defining Class C (Inherits from A)
class C(A):
    pass

C inherits from A

No x is defined in C

So C uses A.x

๐Ÿ“Œ C.x → "A"

๐Ÿ”น 4. Defining Class D (Multiple Inheritance)
class D(B, C):
    pass

D inherits from both B and C

No attribute x is defined in D

Python must decide from which parent to take x

➡️ This is where MRO (Method Resolution Order) comes in

๐Ÿ”น 5. Understanding MRO of Class D

Python calculates MRO using the C3 linearization algorithm:

D.mro()

Result:

[D, B, C, A, object]


๐Ÿ“Œ Meaning:

Python searches attributes in this order:

D

B

C

A

object

๐Ÿ”น 6. Attribute Lookup for D.x
print(D.x)


Step-by-step lookup:

D → ❌ no x

B → ✅ found x = "B"

Stop searching

✅ Final Output
B

Tuesday, 17 February 2026

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

 




Code Explanation:

1. Defining the Decorator Function

def decorator(cls):

This defines a function named decorator.

It takes one argument cls, which will receive a class, not an object.

So this is a class decorator.


2. Adding a Method to the Class

    cls.show = lambda self: "Decorated"

A new method named show is added to the class.

lambda self: "Decorated" creates a function that:

Takes self (the object calling the method)

Returns the string "Decorated"

This method becomes part of the class dynamically.


3. Returning the Modified Class

    return cls

The decorator returns the modified class.

This returned class replaces the original class definition.


4. Applying the Decorator to the Class

@decorator

class Test:

    pass

This is equivalent to writing:

class Test:

    pass

Test = decorator(Test)

The Test class is passed to decorator.

The decorator adds the show() method to Test.


5. Creating an Object and Calling the Method

print(Test().show())

Test() creates an instance of the decorated class.

.show() calls the method added by the decorator.

The method returns "Decorated".


6. Final Output

Decorated

400 Days Python Coding Challenges with Explanation

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

 




Code Explanation:

1. Class Definition
class Demo:
    items = []

This defines a class named Demo.

items is a class variable, not an instance variable.

Class variables are shared by all objects (instances) of the class.

So there is only one items list in memory for the entire class.

2. Creating First Object
d1 = Demo()

This creates an object d1 of class Demo.

d1 does not get its own items list.

Instead, d1.items refers to the same class-level list Demo.items.

3. Creating Second Object
d2 = Demo()

This creates another object d2.

Just like d1, d2.items also refers to the same shared list Demo.items.

4. Modifying the List Using d1
d1.items.append(1)

This appends 1 to the shared items list.

Since the list is shared, the change affects all instances of the class.

Internally, the list now becomes:

items = [1]

5. Printing Using d2
print(d2.items)

d2.items points to the same list that was modified using d1.

Therefore, it prints:

[1]

6. Final Output
[1]


Monday, 16 February 2026

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

 





Code Explanation:

1. Defining the Class

class Test:

This creates a user-defined class named Test.

By default, it inherits from object.

๐Ÿ”น 2. Defining the Constructor (__init__)
def __init__(self, x):
    self.x = x

__init__ runs when a new object is created.

self → refers to the current object

x → parameter passed during object creation

self.x = x → stores the value inside the object

๐Ÿ“Œ Each object gets its own separate x.

๐Ÿ”น 3. Creating the First Object
a = Test(10)

What happens internally:

Memory is allocated for a new object

__init__ is called

self.x becomes 10

๐Ÿ“Œ a now refers to one unique object in memory.

๐Ÿ”น 4. Creating the Second Object
b = Test(10)

Same steps as before

Even though the value 10 is the same, a new object is created

Stored at a different memory location

๐Ÿ“Œ b refers to a different object than a.

๐Ÿ”น 5. Equality Check (==)
a == b

== checks value equality

Since Test does not override __eq__, Python uses:

object.__eq__(a, b)

Default behavior → checks identity, not content

๐Ÿ“Œ Because a and b are different objects → False

๐Ÿ”น 6. Identity Check (is)
a is b

is checks memory location

It returns True only if both variables point to the same object

๐Ÿ“Œ a and b point to different memory locations → False

๐Ÿ”น 7. Printing the Result
print(a == b, a is b)

a == b → False

a is b → False

✅ Final Output
False False

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

 




Code Explanation:

1. Defining the Metaclass
class Meta(type):

type is the default metaclass in Python.

By inheriting from type, Meta becomes a custom metaclass.

A metaclass controls how classes themselves are created.

๐Ÿ“Œ Normal class → creates objects
๐Ÿ“Œ Metaclass → creates classes

๐Ÿ”น 2. Overriding __new__ in the Metaclass
def __new__(cls, name, bases, dct):

This method is called when a class is being created, not when an object is created.

Parameters:

cls → the metaclass (Meta)

name → class name being created ("Test")

bases → parent classes (())

dct → class namespace dictionary ({} initially)

๐Ÿ”น 3. Injecting a Method into the Class
dct["greet"] = lambda self: "Hello"

A new method named greet is dynamically added to the class.

lambda self: "Hello" acts like:

def greet(self):
    return "Hello"


This method becomes part of every class created using Meta.


๐Ÿ”น 4. Creating the Class Object
return super().__new__(cls, name, bases, dct)

Calls type.__new__() to actually create the class

Uses the modified dictionary (now containing greet)

Returns the new class object → Test

๐Ÿ“Œ At this point, Test already has a greet() method.

๐Ÿ”น 5. Defining the Class Using the Metaclass
class Test(metaclass=Meta):
    pass

Python sees metaclass=Meta

Instead of using type, it uses Meta

This triggers:

Meta.__new__(Meta, "Test", (), {})

➡️ greet() gets injected into Test

๐Ÿ”น 6. Creating an Object of the Class
Test()

A normal object is created

No __init__ method exists, so nothing special happens

The object inherits greet() from the class

๐Ÿ”น 7. Calling the Injected Method
print(Test().greet())

Step-by-step:

Test() → creates an instance

.greet() → found in the class (added by metaclass)

self → instance of Test

Returns "Hello"

✅ Final Output
Hello

Sunday, 15 February 2026

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

 



Code Explanation: 

1️⃣ Class Definition

class Tool:


Defines a class named Tool.

A class is a blueprint for creating objects.

2️⃣ Method Definition
    def run(self):
        return "old"


Defines an instance method called run.

self refers to the object that calls the method.

When called normally, run() returns the string "old".

3️⃣ Object Creation
t = Tool()


Creates an object t of the class Tool.

At this moment, t.run() would return "old".

4️⃣ Modifying the Class Method at Runtime
Tool.run = lambda self: "new"


This replaces the run method in the Tool class.

lambda self: "new" is an anonymous function that:

Takes self

Returns "new"

Since methods are looked up on the class, all instances of Tool
now use this new version of run.

⚠️ This change affects:

Existing objects (t)

Future objects created from Tool

5️⃣ Calling the Method
print(t.run())

What happens internally:

Python looks for run on object t

Doesn’t find it on the instance

Finds run on the class Tool

Binds self to t

Executes the lambda function

Returns "new"

✅ Final Output
new


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

 




1️⃣ Class Definition

class Guard:

This line defines a class named Guard.

A class is a blueprint for creating objects.


2️⃣ Overriding __getattribute__

    def __getattribute__(self, name):

__getattribute__ is a special (magic) method in Python.

It is called automatically every time you try to access any attribute of an object.

self → the current object (g)

name → the attribute name being accessed (like "open")

⚠️ This method runs before Python checks normal attributes.


3️⃣ Checking the Attribute Name

        if name == "open":

Python checks if the attribute being accessed is named "open".


4️⃣ Returning a Value

            return "allowed"

If the attribute name is "open", the method returns the string "allowed".

This means g.open will not look for a real attribute—it just returns "allowed".


5️⃣ Raising an Error for Other Attributes

        raise AttributeError

If any other attribute is accessed (like g.close, g.x, etc.),

Python raises an AttributeError.

This tells Python: “This attribute does not exist.”


6️⃣ Creating an Object

g = Guard()

This creates an object g from the Guard class.


7️⃣ Accessing the Attribute

print(g.open)

What happens internally:

Python sees g.open

Calls g.__getattribute__("open")

"open" matches the condition

Returns "allowed"

print() prints it


✅ Final Output

allowed

400 Days Python Coding Challenges with Explanation

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

 




Code Explanation:

1. Defining the Base Class
class Base:

A class named Base is defined.

This class will act as a parent class for other classes.

2. Defining __init_subclass__
    def __init_subclass__(cls):
        cls.tag = cls.__name__.lower()


__init_subclass__ is a special hook method.

Python automatically calls it every time a subclass of Base is created.

cls refers to the new subclass, not Base.

3. Creating Subclass A
class A(Base): pass

What happens internally:

Python creates the class A.

Because A inherits from Base, Python calls:

Base.__init_subclass__(A)


Inside the method:

cls.__name__ → "A"

"A".lower() → "a"

A.tag = "a" is created.

4. Creating Subclass B
class B(Base): pass

What happens internally:

Python creates the class B.

Because B inherits from Base, Python calls:

Base.__init_subclass__(B)

Inside the method:

cls.__name__ → "B"

"B".lower() → "b"

B.tag = "b" is created.

5. Printing the Values
print(A.tag, B.tag)

A.tag → "a"

B.tag → "b"

6. Final Output
a b

✅ Final Answer
✔ Output:
a b

Saturday, 14 February 2026

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

 



Code Explanation:

1. Defining the Class
class Mask:


A class named Mask is defined.

2. Defining a Class Attribute
    y = 10


y is a class attribute.

Normally, accessing m.y would return 10.

๐Ÿ” 3. Overriding __getattribute__
    def __getattribute__(self, name):

__getattribute__ is a special method.

It is called for every attribute access, without exception.

This includes access to:

instance attributes

class attributes

methods

even special attributes

4. Checking the Attribute Name
        if name == "y":
            return 50

If the requested attribute name is "y":

The method immediately returns 50.

Python does not continue normal attribute lookup.

5. Delegating Other Attributes Safely
        return super().__getattribute__(name)

For all attributes except y:

Python calls the original object.__getattribute__.

This avoids infinite recursion.

This ensures normal behavior for other attributes.

6. Creating an Instance
m = Mask()

An object m of class Mask is created.

7. Accessing m.y
print(m.y)

Step-by-step:

Python calls:

Mask.__getattribute__(m, "y")

The condition name == "y" is True.

The method returns 50.

The class attribute y = 10 is never accessed.

8. Final Output
50

✅ Final Answer
✔ Output:
50

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

 


Code Explanation:

1. Defining the Descriptor Class
class D:

A class named D is defined.

This class is going to be used as a descriptor.

2. Defining the __get__ Method
    def __get__(self, obj, owner):
        return 50


__get__ makes this class a descriptor.

This method is called whenever the attribute is accessed.

Parameters:

self → descriptor object (D instance)

obj → instance accessing the attribute (a)

owner → class owning the descriptor (A)

It always returns 50, no matter what.

3. Defining Class A
class A:

A class named A is defined.

4. Attaching the Descriptor to Class A
    x = D()

x is a class attribute.

Its value is an instance of D, so x becomes a descriptor-managed attribute.

5. Creating an Object of Class A
a = A()


An instance a of class A is created.

6. Assigning a Value to a.x
a.x = 10

Python tries to assign 10 to x.

Since the descriptor D does NOT define __set__, this assignment:

Creates an instance attribute:

a.__dict__['x'] = 10

7. Accessing a.x
print(a.x)

Step-by-step attribute lookup:

Python sees that x is a descriptor on class A

Descriptor takes priority over instance attributes

Python calls:

D.__get__(a, A)


__get__ returns 50

The instance value 10 is ignored

8. Final Output
50

✅ Final Answer
✔ Output:
50

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

 



Code Explanation:

1. Defining the descriptor class

class D:

This defines a class named D.

Objects of this class will be used as descriptors.

2. Implementing __get__
    def __get__(self, obj, owner):
        return "D"

__get__ makes D a descriptor.

Parameters:

self → the descriptor object (D()).

obj → the instance accessing the attribute (c).

owner → the class (C).

Whenever x is accessed, this method returns "D".

3. Implementing __set__
    def __set__(self, obj, val):
        pass

Presence of __set__ makes D a data descriptor.

Data descriptors have higher priority than instance attributes.

This method ignores assignments but still controls access.

4. Defining the owner class
class C:

This defines a class named C.

5. Assigning the descriptor
    x = D()

x is a class attribute.

It is managed by the data descriptor D.

6. Creating an instance
c = C()

An object c of class C is created.

Initially, c.__dict__ is empty.

7. Manually inserting an instance attribute
c.__dict__['x'] = "I"

This directly adds an instance attribute x with value "I".

Normally, instance attributes override class attributes…

But not data descriptors.

8. Accessing the attribute
print(c.x)

๐Ÿ” Attribute lookup order (important here):

Data descriptors

Instance attributes (c.__dict__)

Non-data descriptors

Class attributes

x is a data descriptor.

Python calls D.__get__ before checking c.__dict__.

The instance attribute "I" is ignored.

✅ Final Output
D


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

 


Code Explanation:

1. Defining the Class
class Test:

A class named Test is defined.

The class does not define attributes like x or y.

2. Defining the __getattr__ Method
    def __getattr__(self, name):
        return 100

__getattr__ is a special method.

It is called only when an attribute is NOT found normally.

name contains the name of the missing attribute being accessed.

This method always returns 100, regardless of which attribute is missing.

3. Creating an Object
t = Test()

An instance t of class Test is created.

Initially:

t.__dict__ == {}

4. Accessing t.x
t.x

Step-by-step:

Python looks for x in t.__dict__ → ❌ not found

Looks in class Test → ❌ not found

Calls:

__getattr__(t, "x")


__getattr__ returns 100

✅ Result:

100

5. Accessing t.y
t.y

Step-by-step:

Python looks for y in t.__dict__ → ❌ not found

Looks in class Test → ❌ not found

Calls:

__getattr__(t, "y")


__getattr__ returns 100

✅ Result:

100

6. Printing the Values
print(t.x, t.y)

t.x → 100

t.y → 100

7. Final Output
100 100

✅ Final Answer
✔ Output:
100 100

Friday, 13 February 2026

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

 




Code Explanation:

1. Defining the Class
class Flip:

A class named Flip is defined.

2. Defining the Method act
    def act(self):
        self.act = 10
        return 1

Inside this method, two things happen:

๐Ÿ”น Step 1: Assigning to self.act
self.act = 10

This creates an instance attribute named act.

It overrides (shadows) the class method act.

After this line runs, for this instance:

f.act == 10

๐Ÿ”น Step 2: Returning a Value
return 1

The method returns 1.

3. Creating an Object
f = Flip()

An instance f of class Flip is created.

Initially:

f.act → class method

4. First Call: f.act()
f.act()

Step-by-step:

Python looks for act in f.__dict__ → ❌ not found.

Looks in class Flip → finds method act.

Executes the method.

Inside method:

self.act = 10 creates an instance attribute.

Returns 1.

After this call:

f.__dict__ == {'act': 10}


So now:

f.act → 10

5. Printing
print(f.act(), f.act)

๐Ÿ”น First Part → f.act()

Uses the class method.

Returns 1.

๐Ÿ”น Second Part → f.act

Now act is an instance attribute.

Its value is 10.

It is not callable anymore.

So it prints 10.

6. Final Output
1 10

✅ Final Answer
✔ Output:
1 10

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

 


Code Explanation:

1. Defining the Class
class Counter:

A class named Counter is defined.

2. Defining a Class Variable
    total = 0

total is a class variable.

It belongs to the class Counter, not to any specific object.

Initially:

Counter.total == 0

3. Defining the Method inc
    def inc(self):
        self.total += 1

This line is the key trap.

self.total += 1 is equivalent to:

self.total = self.total + 1


Python first reads self.total:

No total in the instance → falls back to Counter.total (0)

Python then assigns to self.total:

This creates a new instance variable named total.

4. Creating Two Objects
a = Counter()
b = Counter()


Two separate instances are created.

At this point:

a.__dict__ == {}
b.__dict__ == {}
Counter.total == 0

5. Calling inc() on a
a.inc()

Step-by-step:

self.total → reads Counter.total → 0

Adds 1 → result 1

Assigns back to instance:

a.total = 1


After this:

a.__dict__ == {'total': 1}
b.__dict__ == {}
Counter.total == 0

6. Printing the Values
print(a.total, b.total, Counter.total)

a.total → instance variable → 1

b.total → no instance variable → uses class variable → 0

Counter.total → class variable → 0

7. Final Output
1 0 0

✅ Final Answer
✔ Output:
1 0 0


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

 


Code Explanation:

1. Defining the Class
class Switch:

A class named Switch is defined.

2. Defining the Method flip
    def flip(self):
        self.flip = lambda: "OFF"
        return "ON"


This method does two important things:

๐Ÿ”น a) Replaces Itself on the Instance
self.flip = lambda: "OFF"


Creates an instance attribute named flip.

This attribute overrides (shadows) the class method flip.

The new value is a lambda function that returns "OFF".

๐Ÿ”น b) Returns a Value
return "ON"

The method returns the string "ON".

3. Creating an Object
s = Switch()

An instance s of class Switch is created.

At this moment:

s.flip → class method

4. First Call: s.flip()
s.flip()

Step-by-step:

Python looks for flip in s.__dict__ → ❌ not found.

Python finds flip in class Switch.

The method is executed.

Inside the method:

self.flip = lambda: "OFF" creates an instance attribute.

The method returns "ON".

After this call:

s.__dict__ == {'flip': <lambda>}

5. Second Call: s.flip()
s.flip()

Step-by-step:

Python looks for flip in s.__dict__ → ✅ found.

The instance attribute (lambda) is used.

The lambda function executes.

Returns "OFF".

6. Printing the Result
print(s.flip(), s.flip())

First s.flip() → "ON"

Second s.flip() → "OFF"

7. Final Output
ON OFF

✅ Final Answer
✔ Output:
ON OFF

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

 


Code Explanation

1. Defining the Class
class Tool:


A class named Tool is defined.

2. Defining Method run
    def run(self):
        return "old"


A method named run is created.

If called normally:

Tool().run() → "old"


At this moment:

Tool.run → original method

3. Creating an Object
t = Tool()


An instance t of class Tool is created.

Important:

The object does NOT store the method inside itself.

It looks up methods dynamically from the class.

4. Modifying the Class at Runtime
Tool.run = lambda self: "new"

The class method run is replaced.

Now:

Tool.run → new lambda function


This change affects:

All future objects

All existing objects

Because method lookup happens at call time, not at object creation time.

5. Calling the Method
print(t.run())

Step-by-step:

Python looks for run in t.__dict__ → ❌ not found.

Looks in class Tool.

Finds the new lambda function.

Executes it.

Returns "new".

6. Final Output
new

✅ Final Answer
✔ Output:
new
400 Days Python Coding Challenges with Explanation

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

 


1. Defining a Metaclass

class Meta(type):

A metaclass named Meta is defined.

It inherits from type, which is the default metaclass in Python.

Metaclasses control class-level behavior.


2. Overriding __instancecheck__

    def __instancecheck__(cls, obj):

        return obj == 1

__instancecheck__ is a special method used by isinstance().

Whenever isinstance(obj, SomeClass) is called:

SomeClass.__instancecheck__(obj)

is executed (if defined).

Here, it ignores the actual type of obj.

It returns:

True only if obj == 1

False otherwise

3. Creating a Class Using the Metaclass

class Test(metaclass=Meta): pass

A class named Test is created.

Instead of the default metaclass (type), it uses Meta.

This means Test inherits the customized __instancecheck__.

4. First isinstance Call

isinstance(1, Test)

Step-by-step:

Python detects that Test has a metaclass with __instancecheck__.

Executes:

Meta.__instancecheck__(Test, 1)

Evaluates:

1 == 1  → True

Result:

True

5. Second isinstance Call

isinstance(2, Test)

Step-by-step:

Python calls:

Meta.__instancecheck__(Test, 2)

Evaluates:

2 == 1 → False

❌ Result:

False

6. Printing the Results

print(isinstance(1, Test), isinstance(2, Test))

Prints both boolean results.

7. Final Output

True False

✅ Final Answer

✔ Output:

True False


900 Days Python Coding Challenges with Explanation

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

 


Code Explanation:

1. Creating an Empty List

names = []

A global list named names is created.

This list will be used to store class names.

2. Defining the Base Class
class Base:

A class named Base is defined.

It will act as a parent class for other classes.

3. Defining __init_subclass__
    def __init_subclass__(cls):
        names.append(cls.__name__)

__init_subclass__ is a special class method.

It is automatically called whenever a subclass of Base is created.

cls refers to the new subclass, not Base.

cls.__name__ gets the name of the subclass.

That name is appended to the names list.

4. Creating Subclass A
class A(Base): pass

What happens internally:

Python creates class A.

Since A inherits from Base, Python automatically calls:

Base.__init_subclass__(A)

"A" is added to names.

Now:

names == ["A"]

5. Creating Subclass B
class B(Base): pass

What happens:

Python creates class B.

Because B also inherits from Base, Python calls:

Base.__init_subclass__(B)


"B" is added to names.

Now:

names == ["A", "B"]

6. Printing the List
print(names)

Prints all collected subclass names.

7. Final Output
['A', 'B']

✅ Final Answer
✔ Output:
['A', 'B']


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

 


Code Explanation:

1. Defining the class
class A:


This line defines a class named A.

2. Overriding __new__
    def __new__(cls):
        return object()


__new__ is responsible for creating and returning a new object.

Instead of creating an instance of A, it returns a plain object instance.

This means the returned object is not of type A.

3. Defining __init__
    def __init__(self):
        print("init")


__init__ initializes an object only if the object returned by __new__
is an instance of the class (A).

Since __new__ returned a plain object, __init__ will not be called.

4. Creating an object
print(A())


A() calls __new__ first.

__new__ returns a plain object.

Python checks whether the returned object is an instance of A.

It is not, so __init__ is skipped.

The print statement prints the default representation of a plain object.

✅ Final Output
<object object at 0x...>

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (202) 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 (18) data management (15) Data Science (290) Data Strucures (16) Deep Learning (119) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (60) Git (9) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (243) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1256) Python Coding Challenge (1034) Python Mistakes (50) Python Quiz (424) 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)