How do I detect and invoke a function when a python enum member is accessed
问题 I have an enum for which some of the members are deprecated: from enum import Enum class Foo(Enum): BAR = "bar" BAZ = "baz" # deprecated How do it get the following behavior: When somebody writes Foo.BAR , everything behaves normally When somebody writes Foo.BAZ , a DeprecationWarning is issued using warnings.warn("BAZ is deprecated", DeprecationWarning) . Afterwards everything behaves normally. The same behavior should apply when members are accessed in other ways, e.g. Foo("baz") and Foo[