Comment 1 for bug 525808

Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote : Re: default Context=False in methods/functions

Nope. Because `{}` is mutable, if it's set as a default argument it "sticks" around for the lifecycle of the function (so until the server is shut down), and if `context` is modified anywhere within the function (or within the functions it calls itself) then next time around the key set in it will appear again:

>>> def foo(k, v, context={}):
... print context
... context[k] = v
... print context
...
>>> foo('a','b')
{}
{'a': 'b'}
>>> foo('c','d')
{'a': 'b'}
{'a': 'b', 'c': 'd'}
>>> foo('e','f')
{'a': 'b', 'c': 'd'}
{'a': 'b', 'c': 'd', 'e': 'f'}

This issue can create pretty subtle bugs (though it's the worst when it impacts appended/extended lists).

In fact, any instance of `context={}` should very much be replaced by `context=None`, not the other way around.