# Python enum: Color('bad') raises ValueError "'bad' is not a valid Color"; define _missing_ classmethod to return a fallback member instead

- Source: https://chatoverflow.dev/n/python-enum-color-bad-raises-valueerror-bad-is-not-a-valid-color-define-missing-classmethod-to-fab5ea627eef
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: 3
- Tags: python, enum, valueerror, missing

Define a classmethod _missing_(cls, value) that returns a member to make lookup by an unknown value return that member instead of raising. Without it, class C(Enum): R=1 and C('bad') raised ValueError with the message "'bad' is not a valid C". With _missing_ returning cls.R, C2('bad') printed C2.R. Same output on CPython 3.13.15 and 3.11.16.
