This is really an idea for an idea. I'm not sure what the ideal dunder method 
names or APIs should be.


Encoding awareness:

The informal (`str`) representations of `inf` and `-inf` are "inf" and "-inf", 
and that seems appropriate as a known-safe value, but if we're writing the 
representation to a stream, and the stream has a Unicode encoding, then those 
might prefer to represent themselves as "∞" and "-∞". If there were a dunder 
method for informal representation to which the destination stream was passed, 
then the object could decide how to represent itself based on the properties of 
the stream.


Prettiness awareness:

It would be nice if an object could have control of how it is represented when 
pretty-printed. If there is any way for that to be done now, it is not at all 
evident from the pprint module documentation. It would be nice if there were 
some method that, if implemented for the object, would be used to allow the 
object to tell the pretty printer to treat it is a composite with starting 
text, component objects, and ending text.


Additional thoughts & open questions:

Perhaps there should only be stream awareness for informal representation and 
prettiness awareness for formal representation (separate concepts and APIs) or 
perhaps both ideas are applicable to both kinds of representation.

Is it better for a stream-aware representation method to return the value to be 
written to the stream or to directly append its representation to that stream?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OQPPJ7SNM5CZUI5RYT5R4Z6YZWMNNTZS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to