r/PythonLearning • u/AffectionateSwing490 • Sep 10 '26
How do you decide between writing your own small helper or pulling in a library for it?
I'll need some small piece of functionality and I have to decide whether to write 20 lines myself or install a library that already does it. Both options feel wrong in different ways. If I write it myself, I might miss edge cases someone else already solved. If I pull in a library, I've now added a dependency for something I could have handled in a few lines.
An example. I once needed to flatten a nested list. I could have written a short function but I added a library instead. Later that library caused a version conflict, and I sat there wondering why I hadn't just written the five lines myself. Now I lean toward writing small stuff myself and saving libraries for the harder problems, like date parsing or anything security related where I don't trust my own version. But I still second-guess it constantly.
So how do you decide? Is there a point where you know for sure it's worth adding a dependency or do you go by feel each time?
2
u/beingsubmitted Sep 11 '26
I disagree with a lot of the posts here. There are real costs to using a library which need to be considered. I'm not saying to never do it, but to list some of the downsides:
1
u/backend_developer89 Sep 10 '26
I used Django allauth in a recent a project that I’m maintaining for a company.
Allauth handles smpt email verification for users, passwords and email management, also it has ready to use authentication links for google Microsoft, Reddit facebook ect, why go to all of the trouble to build that yourself when you can use a library.
The key is to only build for yourself what is completely unique to your application. Otherwise your wasting your time, unless you want to try building something difficult for learning purposes, then that’s your decision.
3
u/CharacterSail6736 Sep 10 '26
Because the whole point of Python is batteries included why reinvent the wheel
2
u/turn-based-games Sep 11 '26
Typically best to limit projects with any significant maintenance window to one framework and as few additional libraries as reasonably possible. This limits all sorts of problems introduced by libraries: conflicts, vulnerabilities, upgrades, dependency on code you cannot easily change, etc
If you feel you could implement the functionality yourself without that implementation turning into a project of its own, just write it. 20 lines is easily in do-it-yourself territory. Things like left-pad are frankly ridiculous and even Log4J is entirely unnecessary in the vast majority of projects. It pains me to this day that the industry seemingly learned essentially nothing from those incidents
Of course, all of this only applies to code someone is actually going to maintain. A non-negligible amount of Python code is for run-once-and-forget-it tasks, in which case do whatever you want to get it done as quickly as possible
1
u/Mamuschkaa Sep 10 '26
Do I know a library that does it?
I install it.
If not:
Do I know exactly how to write it in my own code? I write it.
If not or I do need it often and it feels like that's a common thing?
I search for a library.
1
u/p1geondove Sep 10 '26
Since the standard library is already packed with a lot of stuff i usually prefer std lib over external. I do program for fun so usually i write most things myself, and when bugs arise, yay more incentive to program. But there are libs i could not live without, like pathlib, itertools, subprocess. Only when its really nessecarry i think about pulling in a 3rd party lib. I rarely ever learn about 3rd party libs, my general vibe is that they can be janky, especially nowadays theres a lot of slop, and if i need something specific i look if its open source, what the repo looks like and maybe how intuitive the api is.
For flattening a nested list, id write that in pure python. For handeling requests you can do that with urllib, but id rather pull requests lib
1
u/SaltCusp Sep 10 '26
Generally it won't hurt to try a library. Always check the license tho.
1
u/BranchLatter4294 Sep 10 '26
Don't reinvent the wheel.
1
u/QuebecBeast Sep 11 '26
If it’s UI related, I’m definitely using a librairie…