Python class Vs module: Differences and Comparison

Classes in python are templates for creating objects. They contain variables and functions which define the class objects. At the same time, modules are python programs that can be imported into another python program. Importing a module enables the usage of the module’s functions and variables into another program. Although modules and classes are very different, sometimes one may get confused between their functionalities. In this article, we shall understand the comparison between python class vs module.

Classes in Python

Classes in python act as a blueprint based on which objects are created. Objects are the very basis for object-oriented programming. The objects are the real-world entities, and class acts as a template that defines those objects.

These real-world entities have behavior associated with them, and classes define that behavior. A class contains variables and functions which act on the objects.

To define a class, we use the keyword class to do so. A class can be defined in the following manner:

class class_name:
  var = 'Class variable'

  def class_function1(self):
    #function1 body

How we use a class?

For any object, when we want to define its functionalities, we include that inside the class. The functionalities are characteristics of that object. Let us understand the purpose behind building a class using an example.

We shall define a class named ‘Person’ which will define characteristics of each person – such as name, age, and gender.

class Person:

  def __init__(self,name,age,gender):
    self.name = name
    self.age = age
    self.gender = gender

  def person_details(self):
    print(f'Person Name: {self.name} \nPerson Age: {self.age} \nPerson Gender: {self.gender}\n')

First, we define using the __init__() function – name, age, and gender variables for a given object. We accept four arguments – self, name, age, and gender.

When we do self.name = name, we are actually initializing the argument passed while creating the object to the object itself. It will bind the variable to the current instance of the class. Then, we have another function named ‘person_details()’, which prints the person’s characteristics. It accepts only one argument, which is self.

Now to create the objects, we shall do that in the following manner:

person1 = Person('Andrew',34, 'Male')
person2 = Person('Liam', 21, 'Male')

person1.person_details()
person2.person_details()

We have defined two objects – person1 and person2. Here each person is the real-world entity we are defining using objects. Using the class, we are defining the characteristics of that object. Then, using each object, we are calling the person_details() object.

The output is:

Person Name: Andrew 
Person Age: 34 
Person Gender: Male

Person Name: Liam 
Person Age: 21 
Person Gender: Male

The Entire Code is:

class Person:

  def __init__(self,name,age,gender):
    self.name = name
    self.age = age
    self.gender = gender

  def person_details(self):
    print(f'Person Name: {self.name} \nPerson Age: {self.age} \nPerson Gender: {self.gender}\n')

person1 = Person('Andrew',34, 'Male')
person2 = Person('Liam', 21, 'Male')

person1.person_details()
person2.person_details()

Modules in Python

Modules in Python are files with a .py extension using which we can reuse elements inside that file. When we create a python program, the program may contain inside it functions, variables, and even classes.

If we want to reuse the same piece of function code or the same class, rewriting it would make our code redundant and repetitive. Instead, we can import that entire file as a module into another program.

Doing so makes our code reusable and improves its readability. An entire project can then be broken down into smaller sections, and thus the code becomes more manageable.

How to Access a module?

Python consists of several built-in modules such as math, os, random, CSV, etc. We don’t have to write the function or class for each functionality inside our code to use their functionalities.

Simply import those functionalities by using the import statement. We can either import the entire module or only a particular function or class.

Let us define our own program and import it as a module inside another program.

We shall define the same class person as above inside a python file named ‘Person.py’.

class person:

  def __init__(self,name,age,gender):
    self.name = name
    self.age = age
    self.gender = gender

  def person_details(self):
    print(f'Person Name: {self.name} \nPerson Age: {self.age} \nPerson Gender: {self.gender}\n')

Then, we shall create another python file named ‘test.py’. Inside test.py, we will import the module Person using the import statement.

Then using Person.person, we shall create the object for the class person. Using that object ‘person1’ we will call person_details() function.

import Person
person1 = Person.person('Anna', 20, 'Female')
person1.person_details()

The output is:

Person Name: Anna 
Person Age: 20 
Person Gender: Female

As seen here, by simply importing the module, we could access its functionalities.

We can also import the module and given it another name to access it whenever required.

import Person as p
person1 = p.person('Anna', 20, 'Female')
person1.person_details()

Here the module Person only contains one single class. But let’s say it had several other functions and classes as well. Then to only import the single class Person, we shall use from – import syntax.

from Person import person
person1 = person('Anna', 20, 'Female')
person1.person_details()

This prevents us from unnecessarily importing the entire module when only one of its functionalities is required.

To import all the functionalities of that module, we can use the following statement:

from Person import *

Python Class vs Module

The difference between a class and a module in python is that a class is used to define a blueprint for a given object, whereas a module is used to reuse a given piece of code inside another program.

A class can have its own instance, but a module cannot be instantiated. We use the ‘class’ keyword to define a class, whereas to use modules, we use the ‘import’ keyword. We can inherit a particular class and modify it using inheritance. But while using modules, it is simply a code containing variables, functions, and classes.

Modules are files present inside a package, whereas a class is used to encapsulate data and functions together inside the same unit.


That wraps up the difference between python class vs module. If you have any questions in your mind, don’t forget to leave them in the comments below.

Until next time, Keep Learning!

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
amol
amol
2 years ago

Nice !