- Does python cache or recalculate a function call on a while loop . . .
Python will not cache the value in any way len(s) will be called on every iteration In such looping constructs, there's no point to create a separate variable to store a length, since the calculation is fast for all built-in container types Doing so would be called a premature optimization it recalculates
- Does Python automatically optimize cache function calls?
Trivially: Python, being a dynamically-typed language, doesn't even know the type of 'text' before it actually gets a value, so generalized optimization of this kind is not possible (Some classes may provide their own internal 'cache optimizations', but digressing )
- Make your app fast again: caching function calls in Python
To start simple, you can work with off-the-shelf caching with such as cached_property or lru_cache from the functools module Feel free to combine the different approaches, but here we will focus on the first and second point
|