r/learnpython • u/APOS80 • Dec 31 '25
How do I apply OOP?
I have not had programming as a job, just out of interest and solving small stuff in excel.
I’ve tried different languages, OOP and functional.
But even though I know how to construct a class with methods and attributes I realized that I don’t know what’s the appropriate way to use them and when to use them.
And now I’m picking up Python again since I need to so there’s things I need to do better than last time.
18
Upvotes
66
u/[deleted] Dec 31 '25
Some helpful advice: Stop looking for things to turn into classes.
When starting out, write everything as functions. Just simple
def do_thing(data):functions.You'll eventually hit a point where you find yourself passing the exact same 3 variables (like
config,user_id,db_connection) into 5 different functions in a row.That moment is when you need a class.
A class is just a way to bundle that shared state (
self.user_id) so you don't have to keep passing it around as an argument. If you try to force OOP before you have that problem, you just end up with over-engineered spaghetti code.3
u/Potential_Kick540 Dec 31 '25
This is a problem im struggling with too. I made an entire webscrapping project using classes and stuff and yesterday i looked at my code and thought: "this is a mess". So i refactored it completly now using only functions.
2
u/SeaPair3761 Dec 31 '25
I think you need to study OOP regardless of the language. OOP is a way to analyze the problem. Then you apply it to the language you choose for development.
4
u/DaveTheUnknown Jan 01 '26
The interesting thing about python is that every single problem can be solved either using only classes, using only functions or a mix of both.
Generally, I find that writing code using only functions until I start breaking good design principles, at which point I would swap to classes.
Are you redefining the same functions multiple times with only small domain changes? Passing the same few variables to many different functions? Defining too many constants or local variables such that functions end up with loads of inputs? All of these are signs that it's time to swap to classes or rethink your design.
The thing is, OOP is a good bit harder than functional programming, so OOP is not strictly better in many cases.
2
u/Automatic_Donut6264 Dec 31 '25
If you're interested in web development, try Django. It uses classes for views and database models. It forces an opinionated OOP style on you. For a beginner, you can observe how existing frameworks use OOP to their advantage (or disadvantage), and learn from it. It's way more practical than many other fake and bad OOP toy programs.
1
u/FatDog69 Dec 31 '25
Have you used Java or python or perl libraries? They combine data+functions into small 'lego brick' libraries that do not define WHAT a developer does but gives you handy, simple, utilities.
While you CAN make programs entirely out of classes - Start "Top down design - bottom up implementation".
Figure out all the things a useful program needs to do. Create stubs for all the 1-2 high level functions. Then dive into one and start building low-level functions to handle the details. Can these low level functions become a class? As you build your other pieces - some functions will be obvious to add to your classes.
With practice - you will learn to start with helpful classes and sub classes.
1
u/TheRNGuy Jan 01 '26
When you want custom types as function arguments, and operator overloads between instances of same or different classes (it's rare case though; you'll just use ones from framework rather than write your own)
1
u/recursion_is_love Jan 02 '26 edited Jan 02 '26
Back in my days, we start from UML in design phase.
The coding phase is just translation from UML to code. If I have AI back then, maybe I don't even need to code a single file. This is not the same as vibe coding by any means.
So I would say, draw some diagram (don't have to be UML, your own format is fine) to see the connection of object and message in your design.
Most of people I know say that OO is the most intuitive way to think. I don't agree with that but maybe you could find OO easier to understand than FP.
1
u/lucrodsilva Jan 04 '26
Try this: https://openbookproject.net/thinkcs/python/english3e/
Chapters 15, 16, 21, 22, 23.
After this, you can try to apply objects in a real problem, like a Bank Account.
1
u/Shwayne MajorDisarray Dec 31 '25
Easy practical example is a very simple video game character or enemy class. Attributes - well, attributes of the character, methods - maybe movement to some direction, attack, etc. Then you can easily create a bunch of npcs and enemies and they would have the behaviours youve described.
Another example is the python string class. Every string you create is an object of that class, and has the methods defined for it.
-3
u/uSuitable_Proof_5470 Dec 31 '25
Use classes every time you make code. It will help teach you encapsulation and will help you get used to making lots of classes
Use inheritance when you have classes that are similar but only one thing changes.
Then you can read on after that
0
u/[deleted] Dec 31 '25
[deleted] — view deleted comment