Classes with Module in Python

Last Updated : 16 Feb, 2026

Classes and modules in Python help organise code into reusable and structured components. A module groups related functions, variables and classes into a single file, while classes allow you to create objects with attributes and behaviors. Using them together makes programs more modular, readable, and scalable.

Creating a Class in a Module

Let’s start by creating a module named Agent.py. This module will contain a class called agent with methods to manage the properties of an agent in a game.

  • __init__(): Initializes a new agent with a name and age, sets health to 100 and marks the agent as alive.
  • curr_health(): Displays the agent’s current health.
  • punched(): Reduces the agent’s health by 10.
  • shooted(): Reduces the agent’s health by 50.
  • is_alive(): Updates the agent’s alive status to False if health reaches 0.
  • info(): Prints all details of the agent, including name, age, health and alive status.
Python
#Agent.py
class agent:
    def __init__(self, name, age):
        print('Welcome to the Game')
        self.name = name
        self.age = age
        self.health = 100
        self.alive = True

    def curr_health(self):
        print('Current Health of', self.name, 'is', self.health)

    def punched(self):
        self.health -= 10

    def shooted(self):
        self.health -= 50

    def is_alive(self):
        if self.health == 0:
            self.alive = False

    def info(self):
        print('Name    : ', self.name)
        print('Age     : ', self.age)
        print('Health  : ', self.health)
        print('Alive   : ', self.alive)

Using the Class in Another Module

Now that the Agent.py module is created, let’s import it into another program (app.py) and use the agent class.

  • Importing the Module: import Agent allows access to the Agent class.
  • Creating Objects: Three objects are created: agent_1, agent_2, and boss.
  • Updating Attributes: The health attribute is modified for agent_1 and boss to reflect special power levels.
  • Displaying Information: The info() method prints all details of each agent.
Python
#app.py
import Agent

agent_1 = Agent.agent('Olivia', 32)
agent_1.health = agent_1.health * 100  
print(agent_1.info())
print('-' * 30)

agent_2 = Agent.agent('Harry', 32)
print(agent_2.info())
print('-' * 30)

boss = Agent.agent('Henry', 200)
boss.health *= 1000  
print(boss.info())
print('-' * 30)

Output:

Welcome to the Game
Name : Olivia
Age : 32
Health : 10000
Alive : True
------------------------------
Welcome to the Game
Name : Harry
Age : 32
Health : 100
Alive : True
------------------------------
Welcome to the Game
Name : Henry
Age : 200
Health : 100000
Alive : True
------------------------------

Here:

  • The __init__ method is automatically called when an object is created, printing the welcome message.
  • The info method displays the details of each agent.
  • The health attribute is modified for agent_1 and boss to reflect their special powers.

Extending the Module

The Agent.py module can be extended by adding more classes such as Boss, Animal or others. These new classes can either inherit from existing classes or work independently.

  • Boss inherits from Agent, so it has all attributes and methods of the parent class.
  • Animal is an independent class with its own attributes and methods.
  • This demonstrates code reuse, inheritance, and modular design.
Python
#Agent.py (Extended)
class Boss(agent):
    def blow_fire(self):
        print(f'{self.name} is blowing fire!')

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def info(self):
        print(f'{self.name} is a {self.species}.')
Python
# app.py (Extended)
import Agent

boss = Agent.Boss('Olivia', 200)
boss.blow_fire()
boss.info()

animal = Agent.Animal('Simba', 'Lion')
animal.info()

Output

Olivia is blowing fire!
Name : Olivia
Age : 200
Health : 100
Alive : True
Simba is a Lion.

Advantages of Using Classes with Modules

  • Code Reusability: A class written once inside a module can be used in multiple programs without rewriting the code.
  • Better Organization: Modules help keep related classes and functions together, making the code easier to read and manage.
  • Encapsulation: Classes group data and methods into a single unit, providing a clear and structured design.
  • Easy Scalability: New features, classes, or modules can be added as the project grows without affecting existing code.
Comment
Article Tags:

Explore