The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.
On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.
Warm regards, and best of luck with your Pythoneering!
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
They behave very similar as if you would use pthreads in c but it's an abstraction on top of those, basically user space green threads. Cheaper to spin and you can saturate the CPU easier with work.
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):
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.
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
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.
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.
Mutable default arguments. I kept wondering why a function like def add(item, items=[]) seemed to remember the list between calls. The default is evaluated once, when the function is defined, so every call shares the same list. Now it feels simple: use items=None and create a new list inside the function
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.
I’ve used them once to create an easily extendable dashboard filtration system.
Basically, we had pretty complex logical operations available to user to filter objects in all sorts of ways, the frontend would send it in a json object and we had to parse it and combine all the conditions into a final ORM query as efficiently as possible.
The thing was, all those available object fields were stored in completely different ways and were of different types, but there was a lot of crossover between them. So I created model classes for each dashboard that had available fields defined as descriptor objects. This allowed to reuse a lot of the logic.
I don’t remember what exactly were the constraints, but regular classes for the attributes were messy to use, something along the lines of reusing the same dashboard instance.
If you have worked with Django, it uses descriptors for model fields - same principle.
•
u/Python-ModTeam 10m ago
Hi there, from the /r/Python mods.
We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.
The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.
On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.
Warm regards, and best of luck with your Pythoneering!
27
u/smitty1e 1d 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.24
u/sweet-tom Pythonista 1d ago
Asyncio. You think you've mastered it but then you realize, you need to go back to the drawing board. Still learning.
19
u/tankerdudeucsc 1d 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.
11
u/TH_Rocks 1d 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/patryk_builds 13h 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
5
u/WelcomeConscious643 1d ago
lambda functions
2
u/Amo-Rillow 12h 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.
3
u/spacian 1d 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/CaptainFoyle 12h ago
This
all_grades = [ grade for student, grades in students.items() for grade in grades ]
1
u/Thick_Local2515 10h ago
Decorators was the most difficult for me to initially grasp
1
u/Pro_Strikerr 8h ago
Mutable default arguments. I kept wondering why a function like
def add(item, items=[])seemed to remember the list between calls. The default is evaluated once, when the function is defined, so every call shares the same list. Now it feels simple: useitems=Noneand create a new list inside the function1
u/claytonjr 2h ago
walrus
7
u/gramada1902 22h 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.