I'm facing issue in updating one of the instance variable. Below is the code snippet:
class DataFormatting: def __init__(self): self.default_dict = dict() # default values self.default_model = {'message': 'Please try after some time'} self.default_dict['statusCode'] = 500 self.default_dict['body'] = {'type': 'lead', 'status': self.default_dict['statusCode'], 'model': self.default_model} self.default_dict['headers'] = {'Content-Type': 'application/json'} def get_response(self): return self.default_dict def set_response(self, status_code, final_response): self.default_dict['statusCode'] = status_code self.default_dict['body']['model'] = final_response ob = DataFormatting() ob.set_response(200, {'key1': 'value1'}) ob.get_response() Output:
{'statusCode': 200, 'body': {'type': 'lead', 'status': 500, 'model': {'key1': 'value1'}}, 'headers': {'Content-Type': 'application/json'}} Although the key "statusCode" getting updated as 200, "status" inside "body" is still 500. How should I resolve this?
self.default_dict['body']['status']to anonymously become200without you actually doing that? You are missing that statement to do so.