The ABCs of Python: “with,” “yield” and the magic of egg production

We arrive at our last two Python keywords: with and yield. We could get all esoteric on you with these two words, but we’ll try to keep it simple.

with

Be patient. There’s a payoff. The keyword with  “is used to wrap the execution of a block of code within methods defined by the context manager,” according to programiz. The official documentation states,  “[T]he ‘with‘ statement clarifies code that previously would use try…finally blocks to ensure that clean-up code is executed.”

Got it? If not, that’s okay. The bottom line is that you can use with to toss text into another file and then make sure that file is closed at the end of the process. Try this: create an ordinary text file called “write_to.txt” and put it in the same directory as your other python files. Close that file. Now, use your editor to write the following code and then run it.

with open('write_to.txt', 'w') as a_file:
    a_file.write('I am writing this into the write_to document')
    print("I wrote to 'write_to'")

In addition to printing “I wrote to ‘write_to'” into your shell, this code should print “I am writing this into the write_to document” into (you guessed it) that “write_to.txt” document you just created. To check, go open that doc up and see what’s there.

Presto!

 yield

Okay, this is the last keyword on our list (yay!). Remember return? Well, yield is a more powerful return (sort of). When you use return in a function, you produce just one thing (even if it’s one long list). When you use yield, you can produce a virtual deluge of them if you like. BUT you produce them one at a time so you don’t wind up with some hellaciously large list.

If you’re confused, just hang on a sec. We’ll try to make it pretty concrete.

The keyword yield produces a doo-dad called a generator. A generator, as you might expect, generates stuff. Here’s an example that is modified but based on the elegant one provided by the programiz folks:

def generates_eggs():
    e = "egg "
    for i in range(20):
        yield i*e

g = generates_eggs()
for i in g:
    print(i)

If you run that, you get a lot lines made up of the word “egg.” Each line is a word longer than the previous one because the function using yield generates an ever larger number (up to 19, in this case) of eggs. Therefore, you should get something that looks like this:

egg
egg egg
egg egg egg
egg egg egg egg
egg egg egg egg egg
egg egg egg egg egg egg
egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg
egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg egg

Back in the day, you might have called that concrete poetry. But today we’ll just call it a whole heckova lot of eggs, brought to you by yield.

For a more sophisticated explanation of yield, check out How to Use Generators and yield in Python.

And for much fuller explanation of with, check out The Python “with” Statement by Example.

Featured image by Edithobayaa1: a crate of eggs in Ghana