0

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?

2
  • How can you expect self.default_dict['body']['status'] to anonymously become 200 without you actually doing that? You are missing that statement to do so. Commented Jun 8, 2020 at 6:30
  • @JenilDave: Since I'm a newbie to oops, I thought that updating self.default_dict['statusCode'] = status_code will automatically update self.default_dict['body']['status] because of --> 'status': self.default_dict['statusCode'] Commented Jun 8, 2020 at 6:39

2 Answers 2

2

The status within the body is configured at the creation of the object. To update it when calling 'set_response', you need to include a command for updating body.status as well

 def set_response(self, status_code, final_response): self.default_dict['statusCode'] = status_code self.default_dict['body']['model'] = final_response self.default_dict['body']['status'] = status_code 

The above code change in set_response should be able to fix it. But, you should ideally create an 'update_status' method that takes care of status updates across the object

Sign up to request clarification or add additional context in comments.

2 Comments

I suggest that instead of having the same data item replicated in different places across the object and then using a setter function to make consistent copies, it may usually be preferable to have it stored in once place only, and then move to the getter function (in this case get_response) the task of making any additional copies required by calling code.
Yes. That'd be a better practice. Have status in one place, and get other methods to refer to it. Make copies when needed. Thanks
1

Your DataFormatting object has two data items that are clearly intended to have the same contents as each other, but are stored independently:

self.default_dict['statusCode']

and

self.default_dict['body']['status']

They are set to the same as each other in __init__, but are then allowed to diverge from each other in set_response at the point where one is updated but not the other.

I suggest that your data model is wrong, and each value should be stored in one place, and one place only. For example, you could store it at self.default_dict['body']['status']. If a response then needs to contain a copy of the status at response['statusCode'] and this is not where your object stores it internally, then you could change the get_response method so that instead of simply returning self.default_dict, it makes a copy of it (or possibly a deep copy using copy.deepcopy) and then sets the 'statusCode' item within the copy before returning the copy.

1 Comment

I'd prefer this over my answer, as it is a more robust practice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.