Skip to content

Instantly share code, notes, and snippets.

@alairock
Last active August 14, 2018 16:58
Show Gist options
  • Save alairock/a0a43a74d0391a5ba8cf7aec67104edb to your computer and use it in GitHub Desktop.
Save alairock/a0a43a74d0391a5ba8cf7aec67104edb to your computer and use it in GitHub Desktop.
Generators Yield
import time
# As people enter the queue, take their order, but pause(yield) to make their sandwich
def get_next_customer():
# people are going to be adding themselves to this queue and it will grow quickly
list_of_customers = [('Bob', 'Ham Sandwich'), ('Paul', 'Grilled Cheese'), ('Sally', 'Chicken Croissant')]
for an_order in list_of_customers:
yield an_order
# make a sandwich, but pause(yield) to deliver it
def make_sandwiches():
for customer in get_next_customer():
time.sleep(1) # making the sandwich for our customer
yield (customer[0], customer[1])
sandwiches = make_sandwiches()
# deliver the sandwiches
for sandwich in sandwiches:
print('Delivering sandwich:', sandwich[1], 'for', sandwich[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment