Python generator function
def gen_nums():
n = 0
while n < 4:
yield n
n += 1
Python generator function
def gen_nums():
n = 0
while n < 4:
yield n
n += 1
python generators
# Size of generators is a huge advantage compared to list
import sys
n= 80000
# List
a=[n**2 for n in range(n)]
# Generator
# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
b=(n**2 for n in range(n))
print(f"List: {sys.getsizeof(a)} bitsnGenerator: {sys.getsizeof(b)} bits")
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us