[docs] Code, test, and doc review for PEP-0435 Enum (issue 17947)

mm at ensoft.co.uk mm at ensoft.co.uk
Tue May 14 00:01:07 CEST 2013


http://bugs.python.org/review/17947/diff/8131/Lib/enum.py
File Lib/enum.py (right):

http://bugs.python.org/review/17947/diff/8131/Lib/enum.py#newcode103
Lib/enum.py:103: enum_class = type.__new__(metacls, cls, bases,
classdict)
On 2013/05/13 20:32:37, stoneleaf wrote:
> On 2013/05/13 19:25:49, isoschiz wrote:
> > Shouldn't this technically be a use of super()? Not that I guess
complex type
> > hierarchies of Metaclasses are common.
> 
> __new__ is one of the few things (the only thing?) that cannot be
shared -- you
> can only create an object once.

And yet in this instance you are deferring to your superclass' __new__.
If someone mixes in an extension type with a custom __new__ as well as
Enum, then this will break. Using super() will make that possible.

http://bugs.python.org/review/17947/diff/8131/Lib/enum.py#newcode114
Lib/enum.py:114: if not isinstance(value, tuple):
On 2013/05/13 20:32:37, stoneleaf wrote:
> On 2013/05/13 19:25:49, isoschiz wrote:
> > Doesn't this produce weird behaviour for the following definition:
> > 
> > class MyEnum(Enum):
> >     FOO = 7
> >     BAR = (42, 8)
> >     BAZ = "hello"
> 
> Did you try it?
> """
> --> class MyEnum(Enum): FOO = 7; BAR = (42, 8); BAZ = "hello"
> ... 
> --> MyEnum
> <enum 'MyEnum'>
> --> list(MyEnum)
> [<MyEnum.FOO: 7>, <MyEnum.BAR: (42, 8)>, <MyEnum.BAZ: 'hello'>]
> --> MyEnum.BAR
> <MyEnum.BAR: (42, 8)>
> --> MyEnum.BAR.value == (42, 8)
> True
> """
> 
> Looks good to me. ;)

I must confess I didn't try it - apologies. :-)

Although as a corollary, the code is dense enough that I can't work out
in a straightforward way why this works! Perhaps some more comments
would help?

http://bugs.python.org/review/17947/


More information about the docs mailing list