Python error? Enum and strings - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python error? Enum and strings (/thread-14747.html) |
Python error? Enum and strings - tycarac - Dec-15-2018 Hi To get the output 'first' as the printed enum value that I want , I have to use 'value[0]' instead of 'value'. from enum import Enum class BadEnum(Enum): def __str__(self): return self.value[0] FIRST = 'first', SECOND = 'second' print(BadEnum.FIRST)Is this an error in Python Enum code to use 'tuple' and not 'string'? If I use 'self.value' instead of 'self.value[0]', I get: TypeError: __str__ returned non-string (type tuple) RE: Python error? Enum and strings - Gribouillis - Dec-15-2018 I can't reproduce the bug with python 3.5 in Linux. RE: Python error? Enum and strings - buran - Dec-15-2018 Note that you have a comma at the end of line 7. This way first is actually a tuple , not str . Remove the comma and it will work for BadEnum.FIRST You will not have same error if you try to print BadEnum.SECOND as the code is now
RE: Python error? Enum and strings - tycarac - Dec-15-2018 Hey buran Many thanks. You were right. I feel a bit silly having posted such a simple mistake. |