Why is Python Enums slow?

Today I tried to pass some of my "final" values ​​(members of my class) from the class to Enum. I ran the unittests that I wrote, and noticed that the tests needed a lot longer than before. When I returned these "final" values ​​to the class, everything returned to the old speed again. Here is an example of how I accessed them when they were at Enum:

class SpecialCharacters(Enum):

    TONE_NUMBERS = ["0", "1", "2", "3", "4"]

Access it as follows:

SpecialCharacters.TONE_NUMBERS.value

An example of accessing values ​​when they are in a class:

self.TONE_NUMBERS

So I wonder why my tests take 3 times (!) As much time as I put the values ​​in Enum. It should be a simple call to members of another class, but I don’t think it will make much difference.

( , , , .)

python - 3.4.

+4
2

Python 3.4: https://bugs.python.org/issue23486

"" Python 3.5, enum 3 , , 20x.

+3

Why?:

Enum , . , Enum, , , __getattr__ .

, .

, , ( ).

+3

Source: https://habr.com/ru/post/1569651/


All Articles