When we start learning Python, most of us write everything in one file.
Functions, variables, logic — everything mixed together.
It works for small programs. But when the project grows, the code becomes hard to maintain, difficult to debug, and messy to expand.
So today I decided to rewrite my small Python project using Object-Oriented Programming (OOP).
In this blog I’ll show you:
- Why OOP is important
- How to convert a normal script into OOP
- A simple project structure
- Copy-paste ready code for beginners
This guide is beginner friendly, so you can follow along easily.
Why Use OOP?
Object-Oriented Programming helps organize code into objects and classes.
Instead of writing everything as separate functions, we group related logic into classes.
Benefits of OOP:
✔ Cleaner code structure
✔ Easy to maintain
✔ Code reusability
✔ Easier debugging
✔ Scalable for large projects
Without OOP, projects become messy very quickly.
Recommended Project Structure
A clean Python project should be structured like this:
project/
│
├── main.py
├── user.py
├── post_manager.py
└── utils.py
Each file has one clear responsibility.
-
user.py→ handles user data -
post_manager.py→ handles posts -
utils.py→ helper functions -
main.py→ program entry point
This is a simple structure but it already looks more professional.
Step 1: Create a User Class
Create a file called user.py
class User:
def __init__(self, username, email):
self.username = username
self.email = email
def display_user(self):
print(f"Username: {self.username}")
print(f"Email: {self.email}")
Explanation:
-
__init__()is the constructor -
selfrefers to the current object - The class stores user information
Now we have a User object that represents a real user.
Step 2: Create a Post Manager
Now create post_manager.py
class PostManager:
def __init__(self):
self.posts = []
def create_post(self, content):
self.posts.append(content)
print("Post created successfully!")
def show_posts(self):
print("\nAll Posts:")
for post in self.posts:
print(f"- {post}")
This class manages:
- Creating posts
- Storing posts
- Displaying posts
Instead of mixing everything into one script, we keep post logic inside one class.
Step 3: Utility Functions
Now create utils.py
def show_menu():
print("\nChoose an option:")
print("1. Create Post")
print("2. Show Posts")
print("3. Exit")
Utility files help keep main.py clean.
Step 4: Main Application File
Create main.py
from user import User
from post_manager import PostManager
from utils import show_menu
def main():
username = input("Enter username: ")
email = input("Enter email: ")
user = User(username, email)
manager = PostManager()
print("\nUser Details")
user.display_user()
while True:
show_menu()
choice = input("Select option: ")
if choice == "1":
content = input("Enter post content: ")
manager.create_post(content)
elif choice == "2":
manager.show_posts()
elif choice == "3":
print("Exiting program...")
break
else:
print("Invalid option")
if __name__ == "__main__":
main()
This file connects everything together.
How to Run the Project
Open terminal in your project folder and run:
python main.py
Example output:
Enter username: Alex
Enter email: alex@email.com
Choose an option:
1. Create Post
2. Show Posts
3. Exit
Before vs After OOP
Before using OOP:
❌ Everything in one file
❌ Hard to maintain
❌ Difficult to expand
After using OOP:
✔ Clean file structure
✔ Organized code
✔ Easy to extend
✔ Professional design
Final Thoughts
Refactoring a simple script into an Object-Oriented project is one of the best habits a developer can build.
It helps you:
- Write cleaner code
- Structure projects better
- Think like a software engineer
Even if your project is small, organizing it using classes and modules makes it easier to maintain in the future.
Start simple, keep improving your structure, and your Python projects will feel much more professional.
Conclusion
Turning a messy Python script into a clean OOP structure is a powerful upgrade.
With just a few classes and files, your project becomes:
✔ Cleaner
✔ More maintainable
✔ Easier to scale
If you're learning Python, practicing OOP early will help you build better real-world projects.
Happy coding 🚀
Top comments (0)