Does Python have a default Caching service

1.7k Views Asked by At

Similar to redis and memcache, does Python by default has a caching attached to its run time environment?

This has to be local caching. I need to know without installing or invoking any other libraries, can I cache some data using the existing Python installation in my system. I need to add data to a dict , but before that if there is any default cache, I need to add to that.

Any help is appreciated!

1

There are 1 best solutions below

0
On BEST ANSWER

Depends on what you need to do. If you give a function a default argument that equates to a list or dictionary and never give it an argument for that default, the function parameter could act as a cache. The reason for this is because Python evaluates its function definition at compile time, so the function argument will be loaded in at compile time. Take for example this code:

def foo(value, arr=[]):
    arr.append(value)
    return arr

When ran like so:

for i in range(10):
    foo(i)

the result is:

[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]