A Serialization Mixin

Hard ⏱ 18 min 35% acceptance ★★★★★ 4.5
Write DictMixin providing to_dict() that returns a copy of the instance's __dict__ — then mix it into Product(DictMixin) with name and price. Mixins bundle one reusable behavior and are combined via multiple inheritance without being usable alone.

Examples

Example 1
Input
Product('pen', 10).to_dict()
Output
{'name': 'pen', 'price': 10}
Explanation

The mixin read the instance attributes generically.

Constraints

  • to_dict returns a copy (dict(...)), not the live __dict__.
  • Product gains it purely by inheritance.

Topics

OOPmixins

Companies

Django Software FoundationShopifyStripe

Hints

Hint 1

vars(self) or self.__dict__ holds instance attributes.

Hint 2

The mixin defines no __init__.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.