May-13-2025, 03:57 PM
I wish to have a class that simply contains some values. I want to avoid using the .value attribute. In other words I want to use "class.my_enum" to get a value instead of "class.my_enum.value".
I have the following code that will always return a string.
Something like this would do what I want, except that this would allow the values in the class to be altered.
I have the following code that will always return a string.
from enum import Enum class TestClass(str, Enum): str_value = "abc" byte_value = "abc".encode('latin-1') def __str__(self): return str.__str__(self.value) x = TestClass.str_value print(x) print(type(x)) x = TestClass.byte_value print(x) print(type(x))This returns my values TestClass.str_value and TestClass.byte_value as an enum that works like a string. Unfortunately, I need TestClass.byte_value to be a bytes object.
Something like this would do what I want, except that this would allow the values in the class to be altered.
class TestClass(): str_value = "abc" byte_value = "abc".encode('latin-1') x = TestClass.str_value print(x) print(type(x)) x = TestClass.byte_value print(x) print(type(x)) # This should cause an error TestClass.str_value = 5 print(TestClass.str_value)How can I return different value types and disallow changing of their values?