![]() |
VOOZH | about |
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.
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.
Now that the Agent.py module is created, let’s import it into another program (app.py) and use the agent class.
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 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.
Output
Olivia is blowing fire!
Name : Olivia
Age : 200
Health : 100
Alive : True
Simba is a Lion.