The Setting
I have a data object that comprises a number of properties, let's say:
FaceProperties( eye_color:EyeColorEnum, has_glasses:bool,
nose_length_mm:float, hair_style:str )
- All of these are optional, so instances may only define a subset of them.
- For each single version of the software, there is a fixed set of supported properties (trying to set/get any other would be an implementation error)
- It is quite likely that I need to add a new one to future versions every now and then
What happened so far
In principle, I would very much like to collect them in a dict
, so that I can do all the nice things one can do with a dict
(extend with new keys, iterate over, access by key...). However, an ordinary dict
(and linter) would not notice when an unsupported key is used, e.g. due to some typo, especially with keys that are strings.
Due to these concerns, I started the project using an ordinary class with ordinary properties and implemented all the generic stuff (iteration etc) manually.
Are there better options?
I am currently refactoring the software, and started wondering if a map with a fixed key type (for which I could use a SupportedProperties
Enum
) would solve my problem better, and if there is such a thing in python.
Now, I can simply use the Enum values as keys with an ordinary dict
and probably be reasonably fine, but it would be even nicer if there was some specialized dict
class that would not even accept keys of other types.
Is there maybe some collection (preferably a builtin one) I could use for that? Or would I have to derive my own class from UserDict
and do the type checking myself?