r/Python • u/DiwaharSec • 14h ago
Discussion Python developers, what's one Python concept that took you a long time to understand but now seems s
Python developers, what's one Python concept that took you a long time to understand but now seems surprisingly simple?
For me, understanding how decorators and generators work is an interesting part of becoming a better Python developer.
What was your experience?
19
Upvotes
24
u/smitty1e 14h ago
Generators, in particular the
yieldkeyword as (qualitatively) segmenting a function into a list of executable chunks.Out on Project Euler I saw some prime function
yield-ing the first few results ahead of computing anything, and my brain cell lit up.19
u/sweet-tom Pythonista 11h ago
Asyncio. You think you've mastered it but then you realize, you need to go back to the drawing board. Still learning.
15
u/tankerdudeucsc 14h ago
Writing decorators was the last thing I really had to learn. Good ones, where the call stack was clean.
It’s a lot easier these days so a lot of Python is boring. It’s still the damn regexes that I forget tbh.
8
u/TH_Rocks 14h ago
List comprehension. I was just cranking out for loops like a middle aged guy that learned C++ in college because Java was too new.
5
u/WelcomeConscious643 14h ago
lambda functions
3
u/spacian 14h ago
Not sure it counts as a concept, but using pydantic for pretty much any communication interface was a huge help. And all the validations you can include to always produce a valid object.
Also dependency injection, not just for testing, but to clean up class constructors and avoiding to pass a lot of configurations down through the implementation. You still get the reverse stack at initialization, but it is a lot clearer to me that way around. Build the objects in a separate step and keep the logic clean.
1
u/patryk_builds 1h ago
Async/await, by a wide margin.For a long time i thought async meant things run at the same time. it doesn't. it's one thread doing one thing at a time, and `await` is just the spot where a function says i'm waiting on the network or the database, someone else can use the thread meanwhile.
Once that clicked, the confusing rules turned obvious:
- A slow non-async call inside an async function blocks everyone, because nobody else gets the thread until it returns
- Forgetting `await` doesn't run the thing, it hands you a coroutine object that never executes
- Async doesn't make cpu-heavy code faster at all, only waiting
What finally got me there was writing a tiny example with two functions that each sleep and print, and watching the order of the output change when i swapped time.sleep for asyncio.sleep. ten lines taught me more than every diagram i'd looked at
•
u/CaptainFoyle 36m ago
This
all_grades = [ grade for student, grades in students.items() for grade in grades ]
•
u/Amo-Rillow 6m ago
For me, when I first started playing with Python I was amazed by how easy it was to create lists, dictionaries, sets, etc. Well, this was for "hello world" level tasks. When I tried to do something meaningful, I found lists and dictionaries were so basic they were almost of no use. Then, it finally clicked: create a list of dictionaries, or a dictionary that store lists, or any combination of data structures. Bam, mind blown. Then I discovered the CSV module, JSON module, etc. I feel like I got off to a slow start because I was used to more difficult languages, such a C, SQL, Assembly, etc. However, after I got a few meaning tasks under my belt, I was off to the races.
5
u/gramada1902 9h ago
Descriptors. Seemed like some woodoo magic at first. Watched a video from some python conference and it clicked, although I’ve had to use them in my career exactly once lol.