- Learning Objectives
- Prerequisites
- Introduction
- What is Aliasing?
- Why is Aliasing Required?
- Types of Aliasing
- Module Aliasing
- Function Aliasing
- Class Aliasing
- User-Defined Module Aliasing
- Advantages of Aliasing
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand the concept of aliasing.
- Learn why aliasing is used.
- Create aliases for modules.
- Create aliases for functions.
- Create aliases for classes.
- Create aliases for user-defined modules.
- Follow best practices while using aliases.
Before learning this chapter, you should know:
- Modules
- Import Statements
- Functions
- Classes
Python allows programmers to assign an alternative name to a module, function, or class.
This alternative name is called an alias.
Aliasing makes code shorter, cleaner, and easier to read, especially when working with long module or class names.
Aliasing is the process of assigning another name (an alias) to a module, function, class, or object.
Aliasing means giving a shorter or more convenient name to an existing module, function, or class.
Sometimes module or class names are long.
For example,
import matplotlib.pyplotTyping
matplotlib.pyplotrepeatedly makes the code lengthy.
Instead,
we can create an alias.
import matplotlib.pyplot as pltNow,
plt.plot()is easier to write and read.
Python supports aliasing for:
- Modules
- Functions
- Classes
- User-Defined Modules
A module can be imported using another name.
import module_name as alias_nameimport math as m
print(m.sqrt(64))Output
8.0
Here,
m is the alias for the math module.
Instead of importing an entire module,
a specific function can be imported with an alias.
from module_name import function_name as alias_namefrom math import sqrt as square_root
print(square_root(81))Output
9.0
Classes can also be imported using aliases.
Suppose
student.pycontains
class Student:
passThen,
from student import Student as StdNow,
obj = Std()Custom modules can also be imported with aliases.
Example
import calculator as calcUsage
print(calc.add(10, 20))Aliasing is useful when:
- Module names are very long.
- Function names are lengthy.
- Class names are difficult to type repeatedly.
- Two imported modules have members with similar names.
- Improving readability without changing functionality.
Avoid aliasing when:
- The alias is unclear.
- The original name is already short.
- The alias makes the code difficult to understand.
Bad Example
import math as xBetter Example
import math as mProgram Starts
↓
Import Module
↓
Assign Alias
↓
Python Stores Alias Reference
↓
Program Uses Alias
↓
Original Module Executes
↓
Program Ends
- Reduces typing.
- Improves readability.
- Makes code cleaner.
- Simplifies long module names.
- Makes programs easier to maintain.
- Commonly used in professional projects.
- Use meaningful aliases.
- Keep aliases short and descriptive.
- Avoid single-letter aliases unless they are widely accepted (
m,np,pd,plt). - Do not create confusing aliases.
- Use aliases consistently throughout the project.
Using meaningless aliases.
❌ Incorrect
import math as abcChanging aliases multiple times in the same project.
Creating aliases that conflict with variable names.
Example
import math as total
total = 100This reduces code clarity.
Using aliases unnecessarily.
import os as oSince os is already short and meaningful,
aliasing is usually unnecessary.
Aliasing is the process of assigning another name to a module, function, class, or object.
asYes.
Example
from math import sqrt as square_rootYes.
Classes can also be imported using the as keyword.
To improve readability, reduce typing, and simplify long module or class names.
- Aliasing → Alternative name.
- Keyword →
as - Module alias
- Function alias
- Class alias
- User-defined module alias
- Improves readability.
In this chapter, we learned:
- Aliasing
- Module Aliasing
- Function Aliasing
- Class Aliasing
- User-Defined Module Aliasing
- Advantages
- Best Practices
- Common Mistakes
- Interview Questions
- Quick Revision
Write a Python program to import the math module using an alias.
import math as m
print(m.sqrt(64))
print(m.factorial(5))8.0
120
The math module is imported with the alias
mInstead of writing
math.sqrt()we simply write
m.sqrt()from math import sqrt as square_root
print(square_root(100))10.0
Only the
sqrt()function is imported.
It is assigned the alias
square_rootfrom math import sqrt as root
from math import factorial as fact
print(root(81))
print(fact(6))9.0
720
Multiple functions can be imported with different aliases.
def add(a, b):
return a + bimport calculator as calc
print(calc.add(20, 30))50
The custom module
calculator.py
is imported using the alias
calcclass Student:
def display(self):
print("Student Object")from student import Student as Std
obj = Std()
obj.display()Student Object
The class
Studentis imported using the alias
Stdimport math as m
import random as r
print(m.pow(2, 5))
print(r.randint(1, 10))32.0
7
Both built-in modules are imported using aliases.
import datetime as dt
current = dt.datetime.now()
print(current)2026-07-02 10:45:20.458923
The
datetimemodule is commonly imported using the alias
dtimport os as operating_system
print(operating_system.getcwd())C:\PythonProjects
Modules can be imported using descriptive aliases.
import math as m
radius = 7
area = m.pi * m.pow(radius, 2)
print(area)153.93804002589985
Aliases reduce typing and improve readability in larger programs.
class Employee:
def __init__(self, name):
self.name = name
def display(self):
print("Employee :", self.name)from employee import Employee as Emp
emp = Emp("Rahul")
emp.display()Employee : Rahul
This example demonstrates class aliasing in a real-world scenario.
Program Starts
↓
Import Module
↓
Assign Alias
↓
Alias References Original Module
↓
Program Uses Alias
↓
Original Module Executes
↓
Output Displayed
↓
Program Ends
Python Program
│
├── math Module
│ │
│ └── Alias → m
│
├── calculator Module
│ │
│ └── Alias → calc
│
└── Student Class
│
└── Alias → Std
- Reduces typing.
- Improves readability.
- Makes code cleaner.
- Simplifies long names.
- Frequently used in professional projects.
- Makes importing multiple modules easier.
- Use meaningful aliases.
- Use standard aliases whenever possible (
np,pd,plt, etc.). - Avoid unnecessary aliases for already short names.
- Keep aliases consistent throughout the project.
- Do not use aliases that hide the purpose of a module.
Using meaningless aliases.
❌ Incorrect
import math as xyzUsing different aliases for the same module in different files.
Using aliases that conflict with variable names.
Aliasing every module unnecessarily.
Aliasing is assigning an alternative name to a module, function, class, or object.
asYes.
Example
import math as mYes.
Example
from math import sqrt as rootYes.
Example
from student import Student as StdIt reduces typing, improves readability, and simplifies long module or class names.
No.
It is optional and used for convenience and readability.
asYes.
User-defined modules can also be imported using aliases.
No.
Alias only when it improves readability or reduces lengthy names.
- Import the
mathmodule using an alias. - Import the
randommodule using an alias. - Import a function using an alias.
- Import a class using an alias.
- Create a custom module and import it using an alias.
- Aliasing → Alternative name.
- Keyword →
as - Module alias
- Function alias
- Class alias
- User-defined module alias
- Improves readability.
- Reduces typing.
In this chapter, we learned:
- Aliasing
- Module Aliasing
- Function Aliasing
- Class Aliasing
- User-Defined Module Aliasing
- Practical Programs
- Advantages
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision