Monday, 10 August 2026

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

 


Code Explanation:

๐Ÿ”น 1. Importing NewType
from typing import NewType
✅ Explanation
NewType is imported from Python's built-in typing module.
It is used to create a new logical type based on an existing type.
It improves type checking and makes code easier to understand.

Think of it as giving an existing type a new identity.

typing Module

        │

        ▼

NewType

        │

Create Custom Type

Nothing is created yet.

๐Ÿ”น 2. Creating a New Type
UserId = NewType("UserId", int)
✅ Explanation

A new custom type named UserId is created.

Here,

"UserId" → Name of the new type
int → Base type

This means UserId behaves like an integer but has a different meaning for type checkers.

Current Memory

UserId


Custom Type


Based On int

Think of it as:

int


UserId

It is still an integer internally.

๐Ÿ”น 3. Understanding NewType
NewType("UserId", int)
✅ Explanation

NewType does not create a new class.

Instead, it creates a lightweight function that simply returns the value you pass to it.

Internally, it behaves almost like this:

def UserId(value):
    return value

So there is no extra object created.

Memory Representation

15


UserId()


15

๐Ÿ”น 4. Creating a UserId Object
u = UserId(15)
✅ Explanation

Python passes the value 15 to the UserId type.

Current Memory

u


15

Although we call it UserId, Python actually stores it as a normal integer.

Visual Representation

UserId(15)


15


int

๐Ÿ”น 5. Understanding the Stored Value

Current Situation

u


15
✅ Explanation

u is not a separate object of type UserId.

It is simply an integer value.

That's why Python treats it like this:

u = 15

The custom type name mainly helps static type checkers such as mypy.

๐Ÿ”น 6. Checking the Type
type(u)
✅ Explanation

Python checks the actual runtime type of u.

Current Memory

u


15

Runtime Type

int

Returned object

<class 'int'>

๐Ÿ”น 7. Accessing the Type Name
type(u).__name__
✅ Explanation

type(u) returns

<class 'int'>

The __name__ attribute extracts only the class name.

Result

int

๐Ÿ”น 8. Printing the Result
print(type(u).__name__)
✅ Explanation

Python prints the type name.

Output

int

๐ŸŽฏ Final Output
int

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (337) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (337) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (88) Coursera (302) Cybersecurity (34) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (420) Data Strucures (18) Deep Learning (215) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (387) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1360) Python Coding Challenge (1223) Python Mathematics (11) Python Mistakes (51) Python Quiz (606) Python Tips (100) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (19) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)