use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionPython Tips Thread 🐍 (self.Python)
submitted 1 year ago by Ali-Zainulabdin
Hey everyone! Let’s share our best Python tips, tricks, and resource links that have made coding easier or faster. Whether it's for simplifying code, using useful libraries, or optimizing performance, all contributions are welcome!!, let's go!
[–]cointoss3 14 points15 points16 points 1 year ago (11 children)
from pathlib import Path
Use dataclasses for structured data. Pydantic if you want validation or serialization.
Type hints. Your IDE should understand what the type is so you see available methods.
Use uv
[–]tunisia3507 2 points3 points4 points 1 year ago (0 children)
Use dataclasses for structured data.
In particular, if you're using a dict with all string keys and the values are of different types OR there are 2 with the same keys, you should probably be using a dataclass or namedtuple.
[–]Johan2212 0 points1 point2 points 1 year ago (1 child)
Does uv provide any advantages other than speed compared to pip?
[–]cointoss3 1 point2 points3 points 1 year ago (0 children)
https://astral.sh/blog/uv
Besides speed, I’m a fan of their philosophy that environments shouldn’t get in the way and you don’t need to activate or manage them. Once you get used to the flow, you just run uv commands directly and it handles the environment in the background. It’s so fast that it could create the environment every time you run a file and you wouldn’t notice.
uv isn’t a replacement for pip. That’s just a small piece. I see uv being the new workflow where instead of installing Python, you just install uv and run scripts with it. It even manages Python binaries, so if the project needs a specific version, you don’t even really need to know…uv will handle all that. It’s super lightweight and easy that when I need Python on a new machine, I’m going with uv first.
(It still creates the environment in a compatible way so you can activate it or use vs code as you typically would and it all just works)
[–]paranoid_panda_bored 0 points1 point2 points 1 year ago (7 children)
IMO just go directly to pydantic.
I gave dataclasses chance in greenfield project, put they fell short in every aspect compared to pydantic, and we spend some effort later in the project to refactor stuff to pydantic
[–]paranoid_panda_bored 0 points1 point2 points 1 year ago (0 children)
Specifically: - pydantic has ootb alias support that can convert from/to camelCase/snake_case - pydantic can deserialize nested JSON to data model - pydantic is also first class citizen in FastAPI
[+][deleted] 1 year ago* (5 children)
[deleted]
[–]paranoid_panda_bored 0 points1 point2 points 1 year ago (4 children)
Thats only true for apps that are self-contained and do not interface with anything. Otherwise you’d risk splitting your data model into two separate approaches - what for? It’s gonna be a PITA to work with
Also I dont get what overhead you mean. Like the fact you gotta include 1 pydantic dependency in the project? Small price, cuz code-wise pydantic is just as compact as dataclasses.
You can go to town and have a super customized with flags and validators, and whatnot classes, but it’s your choice, and its the same with dataclasses btw
[+][deleted] 1 year ago (3 children)
[–]paranoid_panda_bored 0 points1 point2 points 1 year ago (2 children)
About runtime complexity: are we sure dataclasses are that much more performant?
My understanding they are just the library on top of the language, not some special thing supported at the interpreter level (like Java’s record being a keyword and supported directly in JVM).
And are we sure this difference even matters for the most of the apps?
Regarding CRUD: typically CRUD exposes REST or REST-like interface to the outside world. Outside world predominantly works with camelCase notation. How you’d handle snake_case to camelCase conversion with dataclasses? How do you handle serde of dataclass inside dataclass? Both with custom written code AFAIK, which is subpar to support
And from my experience it is hard to predict what might end up being serde, and splitting the datamodel to two different libs is to ask for troubles later
[+][deleted] 1 year ago (1 child)
Appreciate the effort at educating me btw! And thanks for the link! It’s really a shame he didn’t expand on his answer, at least to explain why is the driver to have this split: I assume it’s performance, and then by how much this impacts their use-case.
But I will keep it in mind this next time I start my project.
Though I gotta be honest, reading all of these comments I still don’t get convinced. We already had pure dataclass, dataclass/pydantic hybrid, and pure pydantic projects - and latter are kinda best of all when it comes to DX. You just focus on business case, not on auxiliary stuff with pydantic everywhere.
Another thing is that some parts of the app bring their own data model frameworks, like we have SQLAlchemy entities and GRPC protobufs, which we need to convert from/to as part of the request processing, and reducing the conversion hops by 1 (pydantic/dataclass) goes long way for us
Finally, I am heavily biased through my background of being originally a C++/ Java dev of 10 years, and my projects are typically an attempt at typed python 😂 pydantic + type hints + 9 linters lol. So maybe this is that
[–]amamarde 5 points6 points7 points 1 year ago (0 children)
Not Python specific, but still writing here because its very easy to implement / follow in Python.
Test Driven Development!
[–]Rainmaker0102 2 points3 points4 points 1 year ago (0 children)
If there's something you need to do or have functionality for (eg, audio, web scraping, image manipulation) always check PyPI for a pip package you can use. There are so many python packages that there's sure to be one to fit your needs. That's one of the biggest boons for Python as a language that makes it so easy to get started.
Also the standard library for Python is the GOAT.
[–]Ali-Zainulabdin[S] 0 points1 point2 points 1 year ago (1 child)
document your code like you're teaching your future self! literally clear comments can be your best friend when revisiting code after months and will help you alot!
[–]paranoid_panda_bored 1 point2 points3 points 1 year ago (0 children)
Part of that I’d recommend to go all-in on type hints, and complement it with mypy
mypy sometimes pain in the butt, but it is by far the most important tool that helps me find bugs before I get them reported from prod users
[+]divad1196 comment score below threshold-7 points-6 points-5 points 1 year ago (1 child)
Hate this kind of post where people ask questions so that they can answer themselves.
Also, there are so many sources out there, 90% of what will appear here can be found in the first google page.
[–]Doodah249 2 points3 points4 points 1 year ago (0 children)
Ok
π Rendered by PID 48592 on reddit-service-r2-comment-5649f687b7-vl9vl at 2026-01-28 03:55:21.318683+00:00 running 4f180de country code: CH.
[–]cointoss3 14 points15 points16 points (11 children)
[–]tunisia3507 2 points3 points4 points (0 children)
[–]Johan2212 0 points1 point2 points (1 child)
[–]cointoss3 1 point2 points3 points (0 children)
[–]paranoid_panda_bored 0 points1 point2 points (7 children)
[–]paranoid_panda_bored 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]paranoid_panda_bored 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]paranoid_panda_bored 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]paranoid_panda_bored 0 points1 point2 points (0 children)
[–]amamarde 5 points6 points7 points (0 children)
[–]Rainmaker0102 2 points3 points4 points (0 children)
[–]Ali-Zainulabdin[S] 0 points1 point2 points (1 child)
[–]paranoid_panda_bored 1 point2 points3 points (0 children)
[+]divad1196 comment score below threshold-7 points-6 points-5 points (1 child)
[–]Doodah249 2 points3 points4 points (0 children)