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.
> >
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:
>
>
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
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
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
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
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
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
+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
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.
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
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
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
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
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
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
16 matches
Mail list logo