Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sethbunke/0058b3efbe2d9842a316b00b8083309a to your computer and use it in GitHub Desktop.
Save sethbunke/0058b3efbe2d9842a316b00b8083309a to your computer and use it in GitHub Desktop.
Using generators in Python to reduce memory usage
def multiples(input_value=10):
results = []
while 1:
value = input_value if len(results) == 0 else (len(results) + 1) * input_value
results.append(value)
yield results
our_generator = fibonacci()
result = []
for i in range(10):
result = (next(our_generator))
#print(result)
our_multiplier = multiples()
print(our_multiplier)
print(next(our_multiplier))
print(next(our_multiplier))
print(next(our_multiplier))
# x = next(our_generator)
# print(x)
# x = next(our_generator)
# print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment