r/Python • u/RickSore • 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
r/Python • u/RickSore • Nov 14 '17
Like basic looping, performance improvement, etc.
4
u/SupermanIsEnvious Nov 14 '17
Memory.
yieldcreates a generator, which provides the latest result in memory as you operate over an iterator.returncreates 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
yieldwill 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.