[Python-Dev] Fwd: str(IntEnum)

Ethan Furman ethan at stoneleaf.us
Fri Feb 20 19:44:45 CET 2015


On 02/20/2015 10:24 AM, Demian Brecht wrote:

> I think that a decent rule around the usage of __str__ is that it should
> be a string representation of the value, not of the object. Failing the
> ability to logically coerce the value to a string, it should simply fall
> back to repr(obj).

There are two "stringy" methods for objects:  __repr__ and __str__; if __str__ has not been defined __repr__ is
automatically used.

One of the motivating forces behind Enum is that often the name is more important (for humans) than the actual value, so
__str__ is defined as being the Enum class name plus the Enum member name.  The __repr__ has that as well as the
underlying value (occasionally it's useful to know that, too ;) .

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150220/eeff7a66/attachment.sig>


More information about the Python-Dev mailing list