Challenge: Creating Descriptors

Test your knowledge of creating simple descriptor classes in Python.

We'll cover the following

Overview

Create a descriptor class that implements the get, set and delete methods of the descriptor protocol.

Requirements

We currently have a class Temperature that stores a temperature value in Celsius, and contains a function toFahrenheit that returns the temperature value converted to Fahrenheit. You want to create a custom descriptor to aid the Temperature class.

  1. Create a descriptor class TemperatureDescriptor that implements the __get__, __set__, and __delete__ methods of the descriptor protocol, which would work with the Temperature class.

  2. Update the Temperature class so that it correctly utilizes TemperatureDescriptor.

  3. Optional: Add checks to our input value to ensure it is numerical. Should an incorrect value be passed, ValueError can be raised with a custom error message.

Get hands-on with 1200+ tech skills courses.