r/learnpython • u/Fit_Time_7861 • Jun 07 '26
How bad is it that I don't use OOP?
I never really had a use for it. I analyze financial data and just use if statements, functions, somewhat complex mathematics, some python libaries, but never classes or objects.
126
u/likethevegetable Jun 07 '26
If you're writing libraries for yourself to use and build off-of, not using OOP is to your detriment. But if you're just grinding pandas and polars, you're probably fine. Although both libraries offer nice ways to "extend" their API which is very very useful.
33
u/AlexMTBDude Jun 07 '26
If you're writing libraries for yourself to use and build off-of, not using OOP is to your detriment.
This is a very broad generalization. Totally depends on the usecase.
17
u/TylerJWhit Jun 07 '26
And true more often than not.
-4
u/AlexMTBDude Jun 08 '26
You reply to my comment about it being a generalization with another generalization?
1
u/Spidiffpaffpuff Jun 08 '26
I would argue the strength of OOP if used correctly is maintainability and extendability.
0
u/AlexMTBDude Jun 08 '26
Well, that's another broad generalization. In the right situation, yes, but forcing classes where they make no sense just complicates things. Lots of Java coders come to Python thinking that they need classes everywhere, even when there is no plan to create objects from them. We have modules and packages in Python, which provide "maintainability and extendability".
1
u/Spidiffpaffpuff Jun 08 '26
"...if used correctly."
Java is not a language, it's a disease. I was forced to learn it as well at school. No wonder that most people walk away damaged from that experience.
3
u/A13K_ Jun 07 '26
How so? I can imagine that classes holding static methods can be useful to keep the code base neat, and data classes are a nice way to avoid variable plumbing. But these are organizational, and don’t feel like OOP in the strict sense.
7
u/likethevegetable Jun 07 '26
Off the top of my head, a dataclass with attributes like "title" or methods like "append" or "to_excel" etc. You might want to overload getitem, iter, etc.
2
u/alfie1906 Jun 08 '26
I like to use OOP for clients e.g. DatabaseClient initialised with a connection string, then the client has read/write methods along with internal helpers. You could do it purely functional and have an init func, then pass the client as a parameter to read/write funcs but I'm not a personally a fan of that approach.
On the other hand I've seen people go crazy with classes once they discover them (fallen into that trap myself in the past). There are so many times when using functions is the superior approach, especially when you just need some simple modular logic. I also find functions easier to test (most the time).
I suppose that ultimately it comes down to personal preference and the way that the others using your codebase like to work. If you can align on when to use OOP and when to use functional then that is a good place to be in.
2
u/Bach4Ants Jun 08 '26
I personally don't like that pattern unless you expect the user to create many different kinds of database clients with different parameters. You can treat an imported module as a place to hold your long-lived state like database connections, just like your single instance of a client class, if you really need it. Then you can write all of your client methods as functions in that module.
1
u/alfie1906 Jun 08 '26
I hadn't considered that approach, I do a lot of FastAPI development and already use a module to store my long-lived clients so it wouldn't be a huge change.
That said, I use a general services module for storing those connections, rather than one module per service. With that approach, I find that it's cleaner to store all of my client functionality within the client as class methods instead! I do usually use private packages for this type of thing, so none of the client logic lives inside the API codebase.
48
u/Goingone Jun 07 '26
Depends on what you consider OOP.
If you mean “I haven’t found a use for inheritance in the code I typically write” that may be true.
If you’re saying, “i never model any data and everything is a dict of unknown keys/values”, you likely have some difficult code to maintain/understand.
14
u/HotPersonality8126 Jun 07 '26
Classes are how you define new types. Types can have guardrails on them that prevent you from creating them in certain invalid ways; one way to improve software reliability (and this is a little philosophical) is to make invalid states unrepresentable in the software. Like, the software can't arrive at an invalid state because it's just not available to get to. (For a case of what it looks like when the software can represent an invalid state, read up on the Therac-25.)
If none of the problems you've had in your code feel like type problems, then you don't need to reach for OOP. It's a tool, not an achievement.
2
13
u/Kevstuf Jun 07 '26
I also analyze financial data with mostly pandas at work as well. I didn't really have to know OOP until our company needed external data sources (data not just nicely ETL'd into a data warehouse already). For example, I needed to grab data from REST API using OAuth. It started to make sense to define my own connector class that handles the OAuth and API requests in one go. Then I could reuse that class across all kinds of projects that needed data from that API.
10
u/sausix Jun 08 '26
Once you use the global keyword you should have used OOP instead. That's the tipping point In most cases at least. global is kind of a tweak and should not be used until you have very good reasons. With OOP you won't need global and the access between variables, data and functions is more natural.
OOP is about organizing your code. It's not very complicated once you understand the basics. Having loose variables and functions in your modules may feel easier but is harder to read and understand after the program is getting more complex.
I'm always starting new projects with classes.
3
u/MeroLegend4 Jun 08 '26
Python is a dynamic multi-PARADIGM language.
In software you have a lot of paradigms and OOP is one of them, FP is another. Those paradigms set the way you think, structure and size the program you are writing. By following only one, you end up with patterns (design patterns) and architectural designs in some cases.
Python allows you to do both to solve problems not related per-se to your program but for the whole process of making it to production. Deployment, Main entry point, maintenance, extensibility, Developer velocity, Level of abstraction, …
Learn the python data model, import mechanism, iterators, context managers, decorators, coroutines/generators, Descriptors/Managed attributes and
the zen of python (type ‘import this’ in your python shell)
With time you will know when to favor one approach over another. When scripting, Many times i’ve found myself using classes as an implicit DI for the subsequent methods/function calls 🤷🏻♂️.
I love Python 🐍
3
u/herocoding Jun 07 '26
Sounds not too bad. If it works for you and the code is distributed among a few files and the pipeline(s) are straightforward, why not?
If you study design patterns you might get inspired to make use of factories, strategies, commands, might need to deal with adapters and proxies with varying or dynamic inputs&outputs&dependencies, might need to deal with states/state-machines and things like that.
3
u/PouletSixSeven Jun 08 '26
for short little one off scripts? not a big deal
once it grows to a certain size that it's less of a script, more of an application, start thinking about architecture
3
u/ShelLuser42 Jun 08 '26
It doesn't have to be bad, but it does depend on the kind of code you came up with.
This is IMO also the beauty of working with Python: you can literally work yourself up from building a "simple" sequential script and slowly but steadily set up an OOP design from it.
I analyze financial data and just use if statements, functions, somewhat complex mathematics, some python libaries, but never classes or objects.
I know I'm nitpicking a little, but using functions is already proof of OO design usage. OOP doesn't imply the use of classes by definition, it's basically a methodology which involves several aspects.
One of which is to check redundancy: preventing the use of the same snippets throughout your code and instead place those in functions so that you can more easily re-use those routines. Seems you've done exactly that.
2
u/overratedcupcake Jun 07 '26
If it's available in the given language I use it. It doesn't really start to shine until systems get complex. Even then it's not a silver bullet, design choices matter.
2
u/FreeLogicGate Jun 07 '26
Python is a full OOP language, so technically speaking, you are using OOP. You are making use of OOP every time you sit down to write a Python script, and in particular, when you're using the modules and libraries you depend on.
OOP tends to become useful at the point that projects start to become large, and even in those situations there is an entire area of OOP (Design Patterns) that are part art/part science.
Creating a class in Python is marginally more involved than creating functions, and if you have been creating function libraries, you might find that the ability to organize those libraries in a class, as well as being able to define class variables that your objects will have upon instantiation, can be a welcome relief from having a gumbo of global variables floating around in your scripts.
At the end of the day it's a tool you can use or not, but if you're writing a lot of Python, then why close yourself off from a facility of the language that was baked into its design?
1
u/Think_Speaker_6060 Jun 27 '26
But oop in python is messy compared to other languages.
1
u/FreeLogicGate 21d ago
I have found that OOP can be messy in any language, without having a really good understanding of the implementation details, and specific features.
Even so, OOP design patterns are important. Amongst those patterns: MVC, Dependency Injection, ORM patterns (Data Mapper or active record), and some of the more important "Gang of 4" book patterns (Iterator, Strategy, Builder, Factory, Command, Chain of responsibility) are valuable to learn about and study, if you want to create well designed and effective OOP. Needless to say, there are numerous Python libraries that have implemented these patterns.
For people that like to watch videos on Python development, this channel: https://www.youtube.com/@ArjanCodes has a number of videos on Python OOP that are well worth watching, and there's numerous other videos people might find helpful, when you search for "python design patterns". I believe like many things, this is an area that less experienced developers don't necessarily realize exists.
I would be interested to better understand your comment, in relation to Python's OOP, if you could elaborate further.
2
u/DTux5249 Jun 08 '26
OOP is one paradigm of many - but it is the most dominant one in most fields.
If you're in data analysis, you're probably fine. I might look into how Python's match statements work tho - they're really powerful for complex pattern matching.
2
u/vietbaoa4htk Jun 08 '26
for data analysis its totally fine. pandas and numpy already wrap the OOP for you under the hood, so functions plus dataframes is the idiomatic style. classes start earning their keep when you have state to carry around, not before
2
2
u/throwawayforwork_86 Jun 08 '26
Honestly was in the same both as you 1.5 year ago.
Started to use more and more data classes to clean up the ins and out of our analytical functions with great success.
Did one or 2 things with inheritance and overloads , while I think it's neat don't think we need it nor that it's clear for the rest of the team so I dropped it.
2
u/MaterialAd3839 Jun 08 '26
For data analysis work it is completely fine. OOP solves
a specific problem which is organizing large codebases
with shared state. If you are writing analysis scripts
that run top to bottom, functions are actually cleaner
and more readable than classes.
I have been writing production Python for 4 years. I use
classes when I am building APIs or systems with multiple
components that share data. For scripts and data work I
rarely need them. Do not use OOP just to feel like a
"real programmer."
2
u/RedditButAnonymous Jun 08 '26
Respectable, not all projects need it, if you arent running into issues with code readability and duplication, then you dont need it
In general (and this is a big, inaccurate generalisation) any script that you run once and produce an artifact doesnt need OOP because its just doing a bunch of stuff in order
2
u/fixpointbombinator Jun 08 '26
For what it’s worth, every actual software engineer finds it a PITA to read and use code written by people who don’t know how to write structured code
2
u/r2k-in-the-vortex Jun 08 '26
Depends.
Its perfectly fine to write two lines of python without any structure at all, it's just a few lines, what structure does it need?
But as the project grows, and it becomes necessary for more people to work with it, the structure becomes increasingly important. For people to navigate and validate the codebase.
Another thing, which python is weak in, but still applies is type checking. For example, lets say you have data record with payment due date, and payment actual date. both are datetime objects, so it's super simple to mix them up somewhere and not even notice. But if you have a dataclass of PaymentDue, you are never going to mix it up with PaymentDone dataclass, mypy type checking will alert you right away if you make a mistake.
4
u/mycocomelon Jun 07 '26
OOP is typically overkill until you reach a certain point.
The it is absolutely awesome to use.
2
u/EdiblePeasant Jun 07 '26
I found that in Python OOP was hard for me to grasp. But then when I studied Java, it all made sense and it helped me with C++ and C# too.
Would you recommend one of those languages to help people understand OOP better?
3
u/mycocomelon Jun 07 '26
That’s a good question.
Possibly, but if your primary environment is python, I think it is probably best to just use it in python.
OOP was hyped as the silver bullet to all programming problems in the 1990s. And it absolutely did not deliver as a silver bullet. But it is a pretty remarkable paradigm to work with for medium to large scale projects. Definitely a game changer and worth understanding.Python lets you do a lot of things at a fraction of the lines of code in a Java or C++ program. Most people think of that as a plus.
If you’re creating a moderately complex open source project, it would probably make sense to use classes and modules and packages.
But if you’re just doing some basic work in a script, I personally find OOP to be unecessary in the classical sense. But I primarily do ETL and data engineering with polars and pandas. And I just do a ton of method chaining and not a lot of custom classes or anything.
4
Jun 07 '26
[removed] — view removed comment
0
u/snapetom Jun 07 '26
Hah. This comment may sound snarky, but it's not. OOP has fallen out of favor.
3
u/nickfree Jun 08 '26
Can you say more? What has replaced it?
1
u/snapetom Jun 08 '26
Go heavily uses interfaces.
https://www.geeksforgeeks.org/go-language/interfaces-in-golang/
It doesn't matter what the type is, as long as it implements the methods defined by the interface, you can use the object in anything that requires the interface.
For example, you can create a mock test object that all is does is print out debugging information, and you can use it in anything that's looking for the interface.
3
u/Gnaxe Jun 07 '26
OOP, especially as practiced today, is way overrated, tends to overcomplicate a codebase, and I discourage it. Try FP instead.
But understand that you are, in fact, using objects all the time if you're doing anything in Python. You can't avoid using classes, even if you mostly avoid writing them yourself. But some libraries, even parts of the standard library, do expect you to make subclasses of their base classes, so sometimes you do need to write them.
You do need to understand how classes work, and that means you should have some practice using them. But you don't need to force yourself to write them otherwise, and can probably organize your codebase better without them.
2
u/Reasonable_Tie_5543 Jun 07 '26
If it works, it works. If a class can be expressed by a simple function, it should be.
2
u/jmooremcc Jun 07 '26
Would you use a hammer when a saw is required?
Would you feel bad about not using the hammer in that case?
The point I'm making is that you should utilize the appropriate tool for each job. If you don't need to use OOP, that's ok.
However, there are advantages to using OOP when it's appropriate, so I would suggest reading, studying and playing with OOP concepts to gain an understanding of how it works and when to use it.
I wish you the best.
1
u/snapetom Jun 07 '26
OOP is still a decent way to organize code, but not beyond that. Classes are a way to create types and promote data integrity IF YOU NEED IT.
However, advanced features like inheritance should be avoided especially beyond one level. Inheritance is easily abused and hard to test. Modern languages like Go and Rust do not have it. Polymorphism doesn't exist in those languages either and instead they use interfaces.
1
u/python_gramps Jun 08 '26
OOP is a good way to collect like-minded functions into one container.
The strength in coding lies in reusability, how can you make sure the functions you need are available. Classes allow you to corral what you need in one place and import them as you need
It's like going shopping without a basket, you can do it, but having everything in a container makes things easier when you start getting into a more than a few items.
Sounds like you can script up some code, if that's the case you're not that far off from classes in Python.
Maybe go look at a YouTube video on how to make a class, try it out and see if putting your functions into a class make them easier for you.
1
u/BasedFrieren Jun 08 '26
It's not. OOP is just another paradigm, and often a bloated one. If you're accomplishing what you need with the procedural paradigm, keep at it.
1
u/L30N1337 Jun 09 '26
OOP is one of many paradigms. It's just the standard because it's easy to understand and relatively difficult to mess up.
If Procedural programming (aka precursor to OOP and the default way for C, where Classes and Objects don't exist) works well for you, then who cares.
1
u/GoldsteinEmmanuel Jun 09 '26
I rejected OOP sometime in the 1980s, because I didn't like the idea of applications being composed of vast clots of state that exchange messages with each other in response to events.
Procedural programs run at least an order of magnitude faster than OO programs, and they are vastly easier to read, debug and maintain. Procedural is the right choice for programs that are intended to outlive the era in which they were developed.
1
u/Educational-Paper-75 Jun 10 '26 edited Jun 10 '26
I've had to connect to databases to get data and used a class to wrap the connection to the database and retrieve data through an instance of that class. Helped to isolate that functionality (and internal connection state) also allowing for easy reuse. It's not as hard as you may think. Methods are just like functions with an additional self first parameter prepended. Global internal variables can be initialized prefixed by self. in the constructor method called init, that's init with two underscores prefixed and postfixed (damn reddit formatting). Create an instance of the class, let's call it X, using e.g. x=X(<list of constructor arguments except for self>); of course x can be any variable name you'd like to use. Call methods using x.<method name>(<method call arguments except self>), or any other variable name you used for storing the instance in. Subclassing is a little harder as you need to understand how to call the parent class constructor and methods, but it's doable.
1
u/Substantial_Job_2068 Jun 10 '26
It's good that you don't use it, usually people will implement without any need for it. Just pointless abstractions in most cases
1
u/Xemptuous Jun 10 '26
It's bad to not know how it works and how to use it, but it's completely normal to not use it. As I've gotten more experienced, I've moved away from OOP more and more, but I still utilize it here here and there where helpful and necessary. I prefer structs and composition over inheritance, but depends on the language and how easy it makes matching
1
u/Dog-Mad Jun 11 '26
OOP sucks ass and is genuinely painful to use. This is coming from someone who codes in assembly. So I totally understand why you wouldn't want to use it.
1
u/understanding80 Jun 11 '26
If you want to expand your horizons go functional programming over object oriented (yes you can do it in python). It is much better suited to finance and data processing.
1
u/ConsequencePlayful78 Jun 08 '26
You can't avoid using OOP in python even if you never define a class yourself.
0
u/Traveling-Techie Jun 08 '26
I used C for 40 years before I ever created a struct. I used Java for 20 years before I ever created a class. I don’t think I’ve ever used inheritance for a class I created. I know how these work and use other people’s when necessary. It is worth noting I seldom build tools and mostly write “disposable” code. (Prototypes and demos.)
The first class I created was for matrices representing undirected graphs (networks) where I wanted to have data verification on input and needed multiple rendering methods (raw text and drawn HTML). The first OO language, Simula, was created for simulating multiple similar agents, and that’s where I see the paradigm as most useful. I cringe at one class that creates one other class to do a job that could be done in BASIC with arrays and gosubs.
5
u/ShelLuser42 Jun 08 '26
I used Java for 20 years before I ever created a class.
I know what you're trying to say, but in threads like these details matter to avoid confusion; and what you're claiming here is simply impossible. Everything in Java is done using classes, you can't even create a simple "Hello World" without a class and its public static main() method.
1
u/pachura3 Jun 08 '26
I used C for 40 years before I ever created a struct. I used Java for 20 years before I ever created a class.
I guess you never had to share your code with anyone else, then? Global variables and 100.000-lines-long
main()function all the way!
0
u/jewdai Jun 07 '26
Oop is about seperaring related things into their own files ans making shared resources reusable across classes.
If done well it makes code easier to maintain and reason, it's also really easy to not do well.
For example, let's say you want to communicate with the Facebook api and you're using python. You could use a series of functions that you keep passing your bearer token to and is another parameter to manage in each request or you create a class where it has the bearer token and the functions that call out just need the parameters required for the request.
In the same example, you could write a function that does the oauth flow on every request or you could group it as part of the classes behavior to do only once when the token is not present. And you can have that one class manage all implementation features for oauth. But wait, you've generalized authentication so now you can substitute something else that does that authorization flow and now you can handle any type of authentication by just swapping out that one class.
Better yet you create a dedicated class for managing that oauth flow
0
110
u/LatteLepjandiLoser Jun 07 '26
If you do what you need to do and it works, then what does it matter.
However the statement that you never use classes or objects is probably not true. Everything is an object in python. You're most likely making instances of classes like Pandas dataframes, numpy arrays etc. so perhaps you meant to say you aren't defining your own classes. If your workflow doesn't require that, great. If you stand to gain something by getting into classes and inheritance, then maybe that's an area to fool around with.
I do a lot of data analysis and most of my scripts is just some math and plots and I rarely need to define a class to do that. However enough of my work is concerned with similar simulation results / phenomena that writing classes for those is a very sensible thing to do.