joblib/joblib — pull request #82 by AlexandreAbraham (2013-09-10 12:51:14+00:00)
Title: Add a "constant" helper function to avoid unwanted side effects

The most frequent error I have using joblib is caused by deep objects which state changes after function call. This may be innocent in most cases but if the object is used again (typically in a for loop), then it will cause troubles. In this PR, I introduce a "constant" helper which explicitly mark an argument as constant and raises an error in case of side effect.

Code example:

memory = Memory(...)
@memory_cache
def evil_function(innocent_dict, iteration):
innocent_dict['value'] = 2
print iteration

f = memory.cache(evil_function)
a = dict{'value': 1}

# In the following loop, every call is cached, all number are printed
for i in range(10):
f(a, i)

a = dict{'value': 1}
# If I run the same code again, only the second call will be computed (1 will be printed)
# This is obviously an unwanted behavior and it is hard to debug in a big script
for i in range(10):
f(a, i)

a = dict{'value': 1}
# Here, an error is raised in the first loop: I know that my function has an unwanted side-effect
for i in range(10):
f(constant(a), i)