Question: 11. Mixins Mixin classes help add any additional functionalities to some classes using multiple inheritances. Implement two classes DictMixin and JSONMixin, that add a
11. Mixins Mixin classes help add any additional functionalities to some classes using multiple inheritances. Implement two classes DictMixin and JSONMixin, that add a function to_dict and to json respectively. They should convert a Python class to a dictionary and JSON. They should only convert object attributes that do not start with '_' (underscore). If some class is not convertible to JSON, raise a TypeError with the message "Object is not JSON serializable" (without quotes). For Example, name="abc", data = 10, secret = "secret", class MyClass(DictMixin, JSONMixin): definit__(self, name, data, secret): self.name = name self.data = data self._secret = secret obj MyClass("abc", 10, "secret") In the above code obj.to_dict() should return {'name': 'abc', 'data': 10), and obj.to_json() should return {"name": "abc", "data": 10). Implementation Description Complete the class DictMixin and JSONMixin in the editor below. The classes should satisfy the above properties. hackerrank.com Language Pypy 3 1> #!/bin/python3-- 18 11 12 13 14 15 16 # 17 # The class 'JSONMixin' should cotain the # 1. to json 18 19 # 20 21 22 23 24 25 26 27 ) class MyClass(DictMixin, JSONMixin): # # Complete the 'DictMixin' and 'JSONMixin # # The class 'DictMixin' should cotain the # 1. to_dict class DictMixin: # Write your code here class JSONMixin: #Write your code here Test Results I Environment Custom Input
Step by Step Solution
3.38 Rating (154 Votes )
There are 3 Steps involved in it
Based on the images provided you are tasked with implementing two mixin classes in Python DictMixin ... View full answer
Get step-by-step solutions from verified subject matter experts
