r/Python • • Nov 14 '17

Senior Python Programmers, what tricks do you want to impart to us young guns?

Like basic looping, performance improvement, etc.

1.3k Upvotes

629 comments sorted by

View all comments

Show parent comments

4

u/SupermanIsEnvious Nov 14 '17

Memory.

yield creates a generator, which provides the latest result in memory as you operate over an iterator.

return creates a standard function, which operates over an iterator until the operation is completed, then provides a final result.

They’re not always interchangeable, but there are plenty of cases where yield will provide you with flexibility and lower operating cost, such as trying to iterate over 1,000,000 rows of data. Fetching all of those rows at once will probably result in a memory error. A generator allows you to iterate over those 1,000,000 rows and only store one in memory at a time.

2

u/EvM Natural Language Processing Nov 14 '17

And for the same reason you should loop over lines in a file (for line in f:) rather than use f.readlines() whenever possible.