Caching expensive operations in python

Problem You have a function that takes a lot of time to execute. You need to call that function frequently, at different points in your code with the same arguments. This expensive function seems to be the bottleneck. Hopefully, after reading this post you will be able to deal with this situation and make your code work faster. Caching the output You can cache the output of the expensive function using lru_cache....

August 15, 2021 · 3 min

Use named tuples to stop yourself from remembering order of tuples

Let’s consider an example where you’re returning some info about a Person object from a function in the form of a tuple. def info(): ... return name, address, age Now, in this scenario the receiving function needs to know the sequence in which the values are being returned. Although, this might not seem to be much of an issue for a one-time use case, let me try to explain some possible issues with this....

April 15, 2021 · 3 min