25.03.20 21:54, Ethan Furman пише:
Serhiy had the idea of having Enum._convert also modify the __str__ and __repr__ of newly created enumerations to display the module name instead of the enumeration name (https://bugs.python.org/msg325007):
--> socket.AF_UNIX <AddressFamily.AF_UNIX: 1> ==> <socket.AF_UNIX: 1>
--> print(socket.AF_UNIX) AddressFamily.AF_UNIX ==> socket.AF_UNIX
Thoughts?
Thank you for raising this question Ethan. I have several other ideas, maybe not directly related to this. 1. Representation of the value. For IntFlags, values are bit masks, so it is more convenient to represent them as hexidecimals, octals or binaries than decimals. It may be useful for regular enumerations too. It would be nice to have a ways to customize the default representation of the value. For example by setting __value_repr__ = hex. 2. In many case it is more convenient to have a repr in the form Color.RED or even just RED instead of <Color.RED: 'red'>. First, it is shorter and contains all information that you need in the context. It is importatnt if it is a part of the larger object. Second, the result is evaluable, so you can just copy the output and paste it in the Python code, without removing noise. It is especially handy in tests, when you get a collection of enums, or dataclasses with enum fields, or more complex data structure and compare it with expected result. You just write a test, add print() for the actual value, run test, and copy the output as the expected value. So in these cases I want the repr be what the str is now. I do not say that it should be the behavior by default, but maybe change the repr for some stdlib enums for which the literal value is not important?