Python 2 ‘print’, coercing arguments to Unicode
Peter Otten
__peter__ at web.de
Tue Oct 6 18:55:03 EDT 2015
Ben Finney wrote:
>> I don't think this is possible with the print statement, but the
>> print() function can be replaced with anything you like:
>
>
> Hmm. I am more looking for something that doesn't involve replacing
> ‘print’, but rather to hook into whatever it uses to coerce the type of
> its arguments.
Have a look at PyFile_WriteObject in Objects/fileobject.c.
As I understand the code it basically does
if isinstance(obj) and stream.encoding is not None:
s = obj.encode(stream.encoding))
else:
s = str(obj)
stream.write(s)
There's no way to get the original object.
More information about the Python-list
mailing list