[Python-ideas] Re: Enum: determining if a value is valid

2021-03-17 Thread Ricky Teachey
On Wed, Mar 17, 2021, 5:34 AM Serhiy Storchaka wrote: > 12.03.21 23:48, Ethan Furman пише: > > 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. > >

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-17 Thread Serhiy Storchaka
12.03.21 23:48, Ethan Furman пише: > 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: > >  

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Marco Sulla
On Mon, 15 Mar 2021 at 20:49, Ethan Furman wrote: > Everything considered, I think I like allowing `__contains__` to verify both > names and values What about Enum.values()? > adding `default=` to the constructor for the value-based "gimme an > Enum or None" case What's the use case, apart

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Ethan Furman
On 3/16/21 11:43 AM, Matt Wozniski wrote: On Tue, Mar 16, 2021, 2:39 PM Marco Sulla wrote: On Tue, 16 Mar 2021 at 05:38, Matt Wozniski wrote: Color.from_value(1) # returns Color.RED What if I have an alias? Aliases are different names for a single Enum member, so a by-value search is

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Matt Wozniski
That's a problem with any attempt to find an enum member by value, since values aren't guaranteed to be unique. With either proposal, we'd just need to pick one - probably the one that appears first in the class dict. On Tue, Mar 16, 2021, 2:39 PM Marco Sulla wrote: > On Tue, 16 Mar 2021 at

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Marco Sulla
On Tue, 16 Mar 2021 at 05:38, Matt Wozniski wrote: > Color.from_value(1) # returns Color.RED What if I have an alias? ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Guido van Rossum
You have a good point (and as static typing proponent I should have thought of that). Maybe there is not actually a use case for passing an arbitrary default? Then maybe overloading __contains__ (‘in’) might be better? The ergonomics of that seem better for the dominant use case (“is this a valid

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Matt Wozniski
I find the idea of having the constructor potentially return something other than an instance of the class to be very... off-putting. Maybe it's the best option, but my first impression of it isn't favorable, and I can't think of any similar case that exists in the stdlib today off the top of my

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Guido van Rossum
+1 On Mon, Mar 15, 2021 at 12:48 PM Ethan Furman wrote: > On 3/15/21 11:27 AM, Guido van Rossum wrote: > > On Mon, Mar 15, 2021 at 10:53 AM Ethan Furman wrote: > > >> Part of the reason is that there are really two ways to identify an > >> enum -- by name, and by value -- which should

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Ethan Furman
On 3/15/21 11:27 AM, Guido van Rossum wrote: On Mon, Mar 15, 2021 at 10:53 AM Ethan Furman wrote: Part of the reason is that there are really two ways to identify an enum -- by name, and by value -- which should `__contains__` work with? The two sets don't overlap, so we could allow both.

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Ricky Teachey
On Mon, Mar 15, 2021 at 2:28 PM Guido van Rossum wrote: > > ... >> >> I think I like your constructor change idea, with a small twist: >> >> Color(value=, name=, default=) >> >> This would make it possible to search for an enum by value or by name, >> and also specify a default return value

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Guido van Rossum
On Mon, Mar 15, 2021 at 10:53 AM Ethan Furman wrote: > On 3/12/21 5:28 PM, Guido van Rossum wrote: > > On Fri, Mar 12, 2021 at 1:52 PM Ethan Furman 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

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-15 Thread Ethan Furman
On 3/12/21 5:28 PM, Guido van Rossum wrote: On Fri, Mar 12, 2021 at 1:52 PM Ethan Furman 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

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-12 Thread Guido van Rossum
On Fri, Mar 12, 2021 at 1:52 PM Ethan Furman 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

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-12 Thread Ethan Furman
On 3/12/21 2:49 PM, Ricky Teachey wrote: On Fri, Mar 12, 2021 at 4:52 PM Ethan Furman 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. Could

[Python-ideas] Re: Enum: determining if a value is valid

2021-03-12 Thread Ricky Teachey
On Fri, Mar 12, 2021 at 4:52 PM Ethan Furman 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