Python Forum

Full Version: Python error? Enum and strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
I can't reproduce the bug with python 3.5 in Linux.
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
Hey buran

Many thanks. You were right. I feel a bit silly having posted such a simple mistake.