Verifiably better, validated Enum for Python

Ian Kelly ian.g.kelly at gmail.com
Thu May 25 01:09:47 EDT 2017


On Wed, May 24, 2017 at 8:49 PM,  <ofekmeister at gmail.com> wrote:
> On Tuesday, May 23, 2017 at 11:47:21 PM UTC-4, bream... at gmail.com wrote:
>> Well that's what is says here https://github.com/ofek/venum so it must be true.  Please move over Ethan, your time is up :-)
>>
>> Kindest regards.
>>
>> Mark Lawrence.
>
> Hey I've just run some tests and have updated the docs showing memory usage https://github.com/ofek/venum

>From the updated documentation:

"""
This example was run on a 64-bit machine.

Note that stdlib's Enum by itself uses 1056 bytes with each member
requiring 56 bytes, whereas namedtuple Enum uses just 48 bytes with
each member requiring only 16 bytes.

>>> from sys import getsizeof
>>> from enum import Enum as StdEnum
>>> from venum import Enum
>>>
>>> class SomeEnum(StdEnum):
...     BLUE = 3
>>>
>>> getsizeof(SomeEnum)
1056
>>> getsizeof(Enum(('BLUE', 3)))
56
"""

There's a huge problem with this analysis. Specifically, every time
you call Enum, you're creating not just a new namedtuple instance, but
also a new namedtuple singleton class, and you're not accounting for
the size of the class:

>>> Color = Enum(('BLUE', 3), name = 'Color')
>>> Color
Color(BLUE=3)
>>> getsizeof(Color)
56
>>> Color.__class__
<class '__main__.Color'>
>>> getsizeof(Color.__class__)
888

So you're not saving nearly as much memory as you think.



More information about the Python-list mailing list