Merge pull request #164 from harshal-choudhari/python-generators

Add python generator and examples
pull/165/head
Igor Chubin 3 years ago committed by GitHub
commit 340b140789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,20 @@
# Declare a function that behaves like an iterator.
# Any function with yeild returns generator.
# Generators follow lazy evaluation.
# lazy evaluation means that the object is evaluated when it is needed,
# not when it is created.
def my_generator(start, end, step=1):
while start < end:
yeild start
start += step
gen = my_generator(start=1, end=10)
next(gen), next(gen), next(gen)
# (1, 2, 3)
gen = (i for i in range(1, 100) if i%2==0)
next(gen), next(gen), next(gen), next(gen)
# (2, 4, 6, 8)
# https://wiki.python.org/moin/Generators
Loading…
Cancel
Save