Lazy evaluation (called short-circuit evaluation in compiled languages) is a strategy some programming languages use to save work for the last minute or avoid unnecessary work altogether. For example, suppose we had a conditional like this:
Suppose it_is_friday was false. Because of the Python interpreter's lazy evaluation strategy, it wouldn't bother checking the value of it_is_raining—it knows that either way the result of our and will be false, so we won't print the invitation to board game night.
We can use this to our advantage. For example, suppose we have a check like this:
Instead, we could first confirm that 'Becky' and I are still on good terms:
This way, if 'Becky' isn't in friends, Python will lazily ignore the rest of the conditional and avoid throwing the KeyError!
This is all hypothetical, of course. It's not like things with Becky are weird or anything. We're totally cool. She's still in my friends dictionary for sure and I hope I'm still in hers and Becky if you're reading this I just want you to know you're still in my friends dictionary.
Python's generators are also an example of lazy evaluation. For example, the function range in Python generates a list of numbers in a specific range:
This is commonly used for looping. For example, if we wanted to count to some_high_number, we could do this:
But this will generate a list in memory whose size is order of some_high_number! That could be a lot of space.
So instead, we could use a generator. It behaves like a list in that we can loop through it, but instead of building up all of its contents at once, it simply generates the next element right when it's needed (lazily)!
There's a generator version of range in Python: xrange:
In Python 3 they went ahead and made range a generator, so there is no xrange.
We can also take a lazy approach in system design. For example, suppose we had a class for tracking temperatures:
Suppose we wanted to add a feature for getting the the highest temperature we've seen so far. We could "eagerly" keep the max up to date whenever we insert a new temperature:
Or we could lazily (or "just in time") calculate the max whenever it's requested:
The best choice depends on how often you expect to run get_max!
Becky, I haven't hosted another board game night since the incident. I know we both said things we didn't really mean and anyway Becky just if you're reading this please know that I've been cake free for 3 whole days now and it's hard but I'm doing it for you PLEASE Becky. Please.
Pass Your Interviews with My FREE 7-Day Crash Course
I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.
No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe if you hate it.