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.
Create a descriptor class
TemperatureDescriptor
that implements the__get__
,__set__
, and__delete__
methods of the descriptor protocol, which would work with theTemperature
class.Update the
Temperature
class so that it correctly utilizesTemperatureDescriptor
.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 1400+ tech skills courses.