Sunday, 19 July 2026

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

 

Code:

from abc import ABC, abstractmethod class Test(ABC): @abstractmethod def show(self): pass obj = Test()


 



Explanation:

๐Ÿ”น 1. Importing ABC and abstractmethod
from abc import ABC, abstractmethod
✅ Explanation
Python imports two special objects from the abc (Abstract Base Class) module.
ABC is used to create an Abstract Base Class.
@abstractmethod is used to declare methods that must be implemented by child classes.

Think of it like an architect's blueprint.

Architect Blueprint


Must have:

✔ Door
✔ Window
✔ Roof

You cannot live inside a blueprint.

Similarly,

ABC


Defines rules


Cannot be used directly

๐Ÿ”น 2. Creating an Abstract Class
class Test(ABC):
✅ Explanation

Here, Test inherits from ABC.

This tells Python:

"This is not a normal class.

This is an Abstract Class."

Visual:

ABC

 │

 ▼

Test

(Abstract Class)

Unlike a normal class, this class is meant to be inherited, not instantiated.


๐Ÿ”น 3. Using @abstractmethod
@abstractmethod
✅ Explanation

This decorator marks the next method as abstract.

Meaning:

Every child class

MUST

implement this method.

It is like creating a rule.

Example:

School Rule

Every student

must submit homework.

Similarly,

Every child class

must implement show()

๐Ÿ”น 4. Defining the Abstract Method
def show(self):
✅ Explanation

A method named show() is declared.

But notice...

It has no implementation.

It only defines:

Method name
Parameters

Actual logic will be written by child classes.

Visual:

show()


Only Declaration


No Code Yet

๐Ÿ”น 5. Using pass
pass
✅ Explanation

pass means:

"Do nothing."

Python requires every function to have a body.

Since the method is abstract, we leave it empty using pass.

Equivalent idea:

Coming Soon...

No implementation yet.

๐Ÿ”น 6. Current Class Structure

At this point, Python has created:

Test


└── show()

(Abstract Method)

Notice:

show()


No implementation

Therefore the class is incomplete.


๐Ÿ”น 7. Creating an Object
obj = Test()
✅ Explanation

Python now tries to create an object.

Internally:

Create Object


Check Class


Does it contain abstract methods?


YES

Python immediately stops.

๐Ÿ”น 8. Why Does Python Raise an Error?

Because Test still has an abstract method.

Python says:

You promised that

show()

would be implemented,

but it isn't.

So object creation is not allowed.

❌ Error Produced
TypeError:
Can't instantiate abstract class Test
with abstract method show

๐ŸŽฏ Final Output
TypeError:
Can't instantiate abstract class Test
with abstract method show

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (313) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (287) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (33) data (9) Data Analysis (40) Data Analytics (28) data management (16) Data Science (399) Data Strucures (23) Deep Learning (201) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (11) flask (4) flutter (1) FPL (17) Generative AI (76) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (354) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1405) Python Coding Challenge (1202) Python Mathematics (5) Python Mistakes (51) Python Quiz (575) Python Tips (27) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)