Monday, 16 February 2026

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

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (200) 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 (25) Data Analytics (18) data management (15) Data Science (285) Data Strucures (15) Deep Learning (117) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (59) 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 (241) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1254) Python Coding Challenge (1030) Python Mistakes (50) Python Quiz (422) 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)