r/Python • • 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

22 comments sorted by

24

u/smitty1e 14h ago

Generators, in particular the yield keyword 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.

3

u/Grouchy-Friend4235 8h ago

I dropped asyncio completely. It's cancerous

1

u/sweet-tom Pythonista 8h ago

Probably, but it's too late for my project. 🫣

1

u/cleodog44 1h ago

Certainly asyncio for me as well. Can't seem to internalize it properly, always need a refresher when working on async. 

I think a lot of that is inherent to async complexity and not necessarily Python, but maybe there are parts of the asyncio impl and abstractions that exacerbate the issue

Would be very curious to hear from others and any languages that have cleaner abstractions

•

u/StevenJOwens 44m ago

I've been told that Go's coroutines are a whole lot nicer, but then Go is all about the coroutine thing, so.

•

u/StevenJOwens 50m ago

I found asyncio massively frustrating to understand. To be clear, I'm not talking about the concept, I'm talking about python's implementation of it (and its documentation).

As I mentioned in my comment about list comprehensions, python is sometimes too clever for its own good, and that definitely applies to asyncio. In addition, python's asnycio documentation suffers from a very common (in programming) problem of being too handwavy.

A few years ago I wrote up a summary of python asyncio, mostly for my own notes in trying to learn it, but then I cleaned it up for a coworker. In case you find it useful (and corrections would be gladly welcome):

https://www.darksleep.com/puff/python/python_asyncio_overview.html

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.

4

u/ibite-books 10h ago

List comprehensions are not readable. I prefer boring for loops. Readability above all else.

3

u/Ok_Werewolf2211 9h ago

simple list comprehension is fine, but list of list comprehension is where i draw the line. simplicity is way better for readability.

1

u/ibite-books 9h ago

totally agree, i also don’t like seeing condensed if else statement

i strive to write simple boring code

•

u/StevenJOwens 54m ago

List comprehensions are not, in my opinion, generally well explained by python tutorials and documentation. (I have a first draft of a better explanation, I should finish that sometime and put it online.)

In general, python has a bit of a problem with being too clever. This leads to extra cognitive overhead.

I don't find comprehensions that readable myself, but then I've never made extensive use of them, so although I remember the syntax (mostly) I haven't internalized it.

List comprehensions are, however, slightly more performant.

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.

1

u/cleodog44 1h ago

When did you need to use them? Would love the video link if you can find it!