Tags: python
Any Python for loop can be written without using "for".
Seeing how to do this rewrite is the key to understanding how iteration fundamentally works in Python.
Note: PLEASE, DO NOT REPLACE YOUR FOR LOOPS. This video is strictly for understanding what happens under the hood with iter/next/StopIteration stuff under the hood in CPython.
Errata:
- Not *all* for loops raise a StopIteration, only the ones that stop do, i.e. not infinite loops.
β mCoding with James Murphy (https://mcoding.io)
Source code: https://github.com/mCodingLLC/VideosSampleCode
SUPPORT ME β
---------------------------------------------------
Patreon: https://patreon.com/mCoding
Paypal: https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE
Other donations: https://mcoding.io/donate
Top patrons and donors: Jameson, Laura M, Vahnekie, Dragos C, Matt R, Casey G, Johan A, John Martin, Jason F, Mutual Information, Neel R
BE ACTIVE IN MY COMMUNITY π
---------------------------------------------------
Discord: https://discord.gg/Ye9yJtZQuN
Github: https://github.com/mCodingLLC/
Reddit: https://www.reddit.com/r/mCoding/
Facebook: https://www.facebook.com/james.mcoding
MUSIC
---------------------------------------------------
Local Elevator by Kevin MacLeod is licensed under a Creative Commons Attribution 4.0 license. https://creativecommons.org/licenses/by/4.0/
Source: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1300012
Artist: http://incompetech.com/
or
Question
Hello, I was surprised to find out that in python does not support "yield from" inside async generators - according to PEP 525, implementing it "would require a serious redesign of the generators implementation". I think this would make an interesting video if you're interested!
why is it that in a list comp you can get the itterator variable in locals() but not in a normal for loop?
I was expecting you to dive in to list comprehensions, for_each methods ,map methods, filter methods etc.
Why would you add the else with the exception? The break statement already ensures that subsequent code wonβt be executed if a stop iteration is raised. I think decreasing the indent seem cleanerβ¦ π€
no! whilz are redondant, because you can write
for _ in iter(lambda: bool(<condition>, False)):
<body>
Hmmm...in the last example using a print(it) prints out the addresses in memory of your list objects. Maybe it should be print(x) instead?
I've been trying to figure out ways to make as many python keywords as possible redundant, and I'm currently left with def, while (recursion limits :/), await, async, try, except, finally, raise and nonlocal. Would anyone have any ideas on how any of these could be replaced with other features?
iirc for loops dont actually raise StopIteration internally, but instead just iterate until PyIter_Next(iterator) returns a null pointer