Why there is a patch.dict specifically

Question:

I have found that the python internal deals with dictionary object different as the other object like function and list.

Does anyone have idea why python mock library (1.0.1) has a patch.dict specifically besides the existing patch and patch.object?

Asked By: Hello lad

||

Answers:

patch.dict() for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:

foo = {'key': 'value'}
original = foo.copy()

with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
    assert foo == {'newkey': 'newvalue'}

assert foo == original

See the reference for further info.

Answered By: Avinash Babu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.