Skip to content

Instantly share code, notes, and snippets.

@varhub
Created April 20, 2018 09:40
Show Gist options
  • Save varhub/6d01859ad9337f1be40d6fa7ffa70220 to your computer and use it in GitHub Desktop.
Save varhub/6d01859ad9337f1be40d6fa7ffa70220 to your computer and use it in GitHub Desktop.
Python: infinite loop in functional syntax
"""
From Python 3.3, functional programing on top of generators has been reached a new state-level.
Infinite loop without memory footprint with just a `list()` over the magic.
https://stackoverflow.com/questions/13243766/python-empty-generator-function
A new ofuscated way to hide traditional server-loops is here, muajajaja.
"""
def classical_yield():
for i in range(10):
print(i, end=' ')
yield
def empty_yield():
for i in range(10):
print(i, end=' ')
yield from ()
"""
>>> list(classical_yield())
1 2 3 4 5 6 7 8 9
[None, None, None, None, None, None, None, None, None, None]
>>> list(empty_yield())
1 2 3 4 5 6 7 8 9
[]
"""
def forever():
while True:
# [...]
yield from ()
"""
>>> list(forever())
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment