Using Classes Across Files vs Using Functions Across Files

amitmund June 04, 2026

Classes vs Functions Across Files

A very common question is:

If I can import functions from another file, why do I need to import classes?

The answer is:

The import mechanism is almost identical.

The real difference is what you are importing and whether you need to store data (state).


Importing a Function From Another File

math_utils.py

def add(a, b):
    return a + b

main.py

from math_utils import add

result = add(10, 5)

print(result)

Output

15

What Python Does

Open math_utils.py
    ↓
Find add()
    ↓
Import it
    ↓
Use it

Importing a Class From Another File

student.py

class Student:

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

    def show(self):
        print(self.name)

main.py

from student import Student

s1 = Student("John")

s1.show()

Output

John

What Python Does

Open student.py
    ↓
Find Student class
    ↓
Import it
    ↓
Create object
    ↓
Use methods

The Real Difference

Function Import

from math_utils import add

Use immediately:

add(10, 5)

Class Import

from student import Student

Create an object:

s1 = Student("John")

Use methods:

s1.show()

Visual Comparison

Function Import

math_utils.py
│
└── add()
        │
        ▼
main.py
│
└── add(10,5)

Class Import

student.py
│
└── Student
        │
        ▼
main.py
│
├── Student("John")
│
└── s1.show()

Why Use a Function?

Functions are best when you only need behavior.

Example:

def calculate_tax():
    pass
def send_email():
    pass
def convert_temperature():
    pass

No object is needed.

No data needs to be stored.


Why Use a Class?

Classes are best when behavior and data belong together.

Example:

class Student:

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

    def show(self):
        print(self.name)

Each object stores its own data.

s1 = Student("John")
s2 = Student("Alice")
s3 = Student("Bob")

Each object has different state.


Example 1: Calculator

Function Version

def add(a, b):
    return a + b

print(add(10, 5))

Good Choice?

✅ Yes

Why?

Because there is no data to store.


Class Version

class Calculator:

    def add(self, a, b):
        return a + b

Good Choice?

⚠️ Usually unnecessary.

Because the object stores no data.


Example 2: Calculator With History

Class Version

class Calculator:

    def __init__(self):
        self.history = []

    def add(self, a, b):
        result = a + b
        self.history.append(result)
        return result

Usage:

c = Calculator()

c.add(10, 5)
c.add(20, 5)

print(c.history)

Output

[15, 25]

Why Class?

Because each calculator object maintains its own history.


Example 3: Student Management

Function Version

def display_student(name):
    print(name)

Works, but:

display_student("John")
display_student("Alice")

The function doesn't remember anything.


Class Version

class Student:

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

    def display(self):
        print(self.name)

Usage:

s1 = Student("John")
s2 = Student("Alice")

Each object stores its own information.


Example 4: Bank Account

Function Version

balance = 1000

def deposit(amount):
    pass

def withdraw(amount):
    pass

This becomes difficult when handling multiple accounts.


Class Version

class BankAccount:

    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount

Usage:

a1 = BankAccount(1000)
a2 = BankAccount(5000)

Each account maintains its own balance.

Why Class?

Because each account has separate state.


Example 5: Temperature Converter

Function Version

def celsius_to_fahrenheit(c):
    return (c * 9/5) + 32

Good Choice?

✅ Yes

No state required.

No object required.


Example 6: File Utilities

Function Version

def read_file(path):
    pass

def write_file(path, data):
    pass

Good Choice?

✅ Yes

These are utility operations.

No stored data needed.


Example 7: Employee System

Class Version

class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def display(self):
        print(self.name, self.salary)

Usage:

e1 = Employee("John", 50000)
e2 = Employee("Alice", 70000)

Why Class?

Each employee stores unique data.


Example 8: Shopping Cart

class ShoppingCart:

    def __init__(self):
        self.items = []

    def add_item(self, item):
        self.items.append(item)

Usage:

cart1 = ShoppingCart()
cart2 = ShoppingCart()

Each cart has different items.

Why Class?

Because each cart has its own state.


Function vs Class Comparison

Feature Function Class
Stores Data ❌ No ✅ Yes
Creates Objects ❌ No ✅ Yes
Maintains State ❌ No ✅ Yes
Reusable Behavior ✅ Yes ✅ Yes
Best for Utilities ✅ Yes ❌ Usually No
Best for Real-World Entities ❌ No ✅ Yes

Visual Model

Function

Input
  ↓
Function
  ↓
Output

No memory.

No stored state.


Class

Data (State)
     +
Methods (Behavior)
     ↓
Object

The object remembers information.

Example:

self.name
self.balance
self.history

These values stay with the object.


Real-World Analogy

Function

A calculator on a website:

Enter Numbers
      ↓
Calculate
      ↓
Show Result

The calculator does not remember previous calculations.


Class

A bank account:

Account
│
├── Account Number
├── Balance
├── Owner
└── Transactions

The account must remember information.

So a class is a better choice.


Rule to Remember

Need only behavior?
    ↓
Use Functions

Need behavior + stored data?
    ↓
Use Classes

Final Takeaway

Importing a class from another file is technically done the same way as importing a function:

from module import something

The difference is:

  • Functions provide behavior.
  • Classes provide behavior + data.
  • Classes allow creation of multiple objects with their own state.
  • Functions are usually best for utility tasks.
  • Classes are usually best for modeling real-world entities.
0 Likes
5 Views

Filters

No filters available for this view.

Reset All