Python- Day 31- CLASSES-Importing Classes

Nidhi Ashtikar
3 min readDec 25, 2024

--

Importing a Single Class from a Module

Keep files organized and uncluttered by placing classes in separate modules.

Example: Create a car.py file for the Car class.

# car.py
#A class that can be used to represent a car.

class Car:
#A simple attempt to represent a car.

def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()

def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")

Import and use the Car class in my_car.py

# my_car.py
from car import Car

my_new_car = Car('audi', 'a4', 2024)
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading = 23
my_new_car.read_odometer()


>>

2024 Audi A4
This car has 23 miles on it.

Storing Multiple Classes in a Module:

Group-related classes within the same module for better organization.

Example: In car.py, add Battery and ElectricCar classes, which are related to Car

# car.py
#A set of classes used to represent gas and electric cars.

class Car: # Car class definition as above

class Battery:
#A simple attempt to model a battery for an electric car.

def __init__(self, battery_size=40):
self.battery_size = battery_size

def describe_battery(self):
print(f"This car has a {self.battery_size}-kWh battery.")

def get_range(self):
range = 150 if self.battery_size == 40 else 225
print(f"This car can go about {range} miles on a full charge.")

class ElectricCar(Car):
#Models aspects of a car, specific to electric vehicles.

def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = Battery()

In my_electric_car.py, import ElectricCar

# my_electric_car.py
from car import ElectricCar

my_leaf = ElectricCar('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())
my_leaf.battery.describe_battery()
my_leaf.battery.get_range()

>>

2024 Nissan Leaf
This car has a 40-kWh battery.
This car can go about 150 miles on a full charge.

Importing Multiple Classes from a Module:

Import several classes at once from a module when using multiple classes from the same module.

# my_cars.py
from car import Car, ElectricCar

my_mustang = Car('ford', 'mustang', 2024)
print(my_mustang.get_descriptive_name())

my_leaf = ElectricCar('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())

>>

2024 Ford Mustang
2024 Nissan Leaf

Importing an Entire Module:

Import the entire module and access classes with dot notation to avoid naming conflicts.

# my_cars.py
import car

my_mustang = car.Car('ford', 'mustang', 2024)
print(my_mustang.get_descriptive_name())

my_leaf = car.ElectricCar('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())

>>

2024 Ford Mustang
2024 Nissan Leaf

Importing All Classes from a Module Using Wildcard:

Syntax:

from module_name import *

Note: Avoid using * as it can cause naming conflicts and reduce code readability.

Importing a Module into Another Module:

Spread classes across modules for large projects, allowing one module to access classes from another.

Example: Store Car in car.py and ElectricCar and Battery in electric_car.py.

# car.py
#A class that can be used to represent a car.

class Car:
# Car class definition

# electric_car.py
#A set of classes for electric cars.
from car import Car

class Battery:
# Battery class definition

class ElectricCar(Car):
# ElectricCar class definition

Import both modules into my_cars.py

# my_cars.py
from car import Car
from electric_car import ElectricCar

my_mustang = Car('ford', 'mustang', 2024)
print(my_mustang.get_descriptive_name())

my_leaf = ElectricCar('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())

Using Aliases for Classes and Modules:

Simplify long class or module names by assigning shorter aliases.

# my_cars.py
from electric_car import ElectricCar as EC

my_leaf = EC('nissan', 'leaf', 2024)
print(my_leaf.get_descriptive_name())

# Using a module alias
import electric_car as ec

my_leaf = ec.ElectricCar('nissan', 'leaf', 2024)

Tip: Bgin with simple structures in one file, and as your code grows, move classes into modules for better organization.

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet