Enum class

Peter Otten __peter__ at web.de
Thu Oct 15 11:11:49 EDT 2015


Joseph L. Casale wrote:

>>>>> import enum
>>>>> class M(enum.EnumMeta):
>>...     def __contains__(self, value):
>>...         print("checking", value)
>>...         return super().__contains__(value)
>>...
>>>>> class Colors(enum.Enum, metaclass=M):
>>...     red = 1
>>...     green = 2
>>...     blue = 3
>>...
>>>>> Colors.red in Colors
>>checking Colors.red
>>True
>>
>>Is that what you're asking for? If not can you give an example?
> 
> Hi Peter,
> That is exactly what I was referring to, I just wondered if I overlooked
> some way to apply it after the initial meta class built the instance. It
> appears not...

Like that?

>>> class M2(enum.EnumMeta):
...     def __contains__(self, value):
...         print(value, "? never", sep="")
...         return False
... 
>>> Colors.__class__
<class '__main__.M'>
>>> Colors.red in Colors
checking Colors.red
True
>>> Colors.__class__ = M2
>>> Colors.red in Colors
Colors.red? never
False





More information about the Python-list mailing list