On Fri, Mar 12, 2021 at 1:52 PM Ethan Furman <et...@stoneleaf.us> wrote:
> A question that comes up quite a bit on Stackoverflow is how to test to > see if a value will result in an Enum member, preferably without having to > go through the whole try/except machinery. > > A couple versions ago one could use a containment check: > > if 1 in Color: > > but than was removed as Enums are considered containers of members, not > containers of the member values. Maybe you were a bit too quick in deleting it. Was there a serious bug that led to the removal? Could it be restored? > It was also possible to define one's own `_missing_` method and have it > return None or the value passed in, but that has also been locked down to > either return a member or raise an exception. > > At this point I see three options: > > 1) add a `get(value, default=None)` to EnumMeta (similar to `dict.get()` > But the way to convert a raw value to an enum value is Color(1), not Color[1], so Color.get(1) seems inconsistent. Maybe you can just change the constructor so you can spell this as Color(1, default=None) (and then check whether that's None)? > 2) add a recipe to the docs > But what would the recipe say? Apparently you're looking for a one-liner, since you reject the try/except solution. > 3) do nothing > Always a good option. :-) Where's that StackOverflow item? How many upvotes does it have? -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/QRDMKZBL54GU7A7OSDB5GG2HIAGCDTLE/ Code of Conduct: http://python.org/psf/codeofconduct/