Am 01.05.2013 20:44, schrieb Antoine Pitrou:
> On Wed, 01 May 2013 10:21:30 -0700That's exactly what's implemented in the ref435 code at the moment.
> Ethan Furman <et...@stoneleaf.us> wrote:
>> We may not want to /completely/ disallow subclassing. Consider:
>>
>> --> class StrEnum(str, Enum):
>> ... '''string enums for Business Basic variable names'''
>> ...
>> --> class Vendors(StrEnum):
>> EnumError: subclassing not allowed
>
> I don't see the point of disallowing subclassing. It sounds like
> a pointless restriction.
>
> However, perhaps the constructor should forbid the returning of a base
> type, e.g.:
>
> class Season(Enum):
> spring = 1
>
> class MySeason(Season):
> """I look nicer than Season"""
>
> MySeason('spring')
> ...
> ValueError: Season.spring is not a MySeason instance
>
> (what this means is perhaps the subclassing of non-empty enum classes
> should be forbidden)
Am 01.05.2013 20:04, schrieb Eli Bendersky:
Wait a moment... it might not be immediately useful for IntEnums (however,
> Actually, in flufl.enum, IntEnum had to define a magic __value_factory__
> attribute, but in the current ref435 implementation this isn't needed, so
> IntEnum is just:
>
> class IntEnum(int, Enum):
> '''
> Class where every instance is a subclass of int.
> '''
>
> So why don't we just drop IntEnum from the API and tell users they should do the
> above explicitly, i.e.:
>
> class SocketFamily(int, Enum):
> AF_UNIX = 1
> AF_INET = 2
>
> As opposed to having an IntEnum explicitly, this just saves 2 characters
> (comma+space), but is more explicit (zen!) and helps us avoid the special-casing
> the subclass restriction implementation.
that's because base Enum currently defines __int__ which I find questionable),
but with current ref435 you *can* create your own enum base classes with your
own methods, and derive concrete enums from that. It also lets you have a
base class for enums and use it in isinstance().
If you forbid subclassing completely that will be impossible.
On Wed, 1 May 2013 13:57:11 -0700Because I may want to share methods accross all concrete subclasses of
Eli Bendersky <eli...@gmail.com> wrote:
>
> I still don't understand what you mean, sorry. Like, this:
>
> class MyEmptyEnum(Enum):
> pass
>
> Why would you want to subclass MyEmptyEnum ?
>
> Or do you mean this:
>
> class IntEnum(int, Enum):
> pass
>
> Now I can have:
>
> class SocketFamily(IntEnum):
> ??
>
> If it's the latter, then why allow subclassing explicitly just for this
> reason?
IntEnum (or WhateverEnum).
Am 01.05.2013 23:48, schrieb Eli Bendersky:
OK, I'm stupid -- I was thinking about moving the __int__ method to IntEnum
> Well, my point is that you currently don't have to inherit from int (or IntEnum)
> to get an __int__ method on your Enum, which is what I find questionable. IMO
> conversion to integers should only be defined for IntEnums. (But I haven't
> followed all of the discussion and this may already have been decided.)
>
>
> Good point. I think this may be just an artifact of the implementation - PEP 435
> prohibits implicit conversion to integers for non-IntEnum enums. Since IntEnum
> came into existence, there's no real need for int-opearbility of other enums,
> and their values can be arbitrary anyway.
(that's why I brought it up in this part of the thread), but as a subclass of
int itself that obviously isn't needed :)
The reason __int__ is there is because pure Enums should be using plain ints as their value 95% or more of the time, and being able to easily convert to a real int for either database storage, wire transmission, or C functions is a Good Thing.
Good point. I think this may be just an artifact of the implementation - PEP 435 prohibits implicit conversion to
integers for non-IntEnum enums. Since IntEnum came into existence, there's no real need for int-opearbility of other
enums, and their values can be arbitrary anyway.
Ethan - unless I'm missing something, __int__ should probably be removed.
On May 01, 2013, at 03:11 PM, Ethan Furman wrote:But then, Foo.a.value is good enough.
>The reason __int__ is there is because pure Enums should be using plain ints
>as their value 95% or more of the time, and being able to easily convert to a
>real int for either database storage, wire transmission, or C functions is a
>Good Thing.