Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97460

Re: Python 2 ‘print’, coercing arguments to Unicode

From Peter Otten <__peter__@web.de>
Subject Re: Python 2 ‘print’, coercing arguments to Unicode
Date 2015-10-07 00:55 +0200
Organization None
References <851td81g08.fsf@benfinney.id.au> <85wpv0z368.fsf@benfinney.id.au> <201510061248.t96CmCWP011700@fido.openend.se> <85si5nzo6y.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.436.1444172118.28679.python-list@python.org> (permalink)

Show all headers | View raw


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.



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 2 ‘print’, coercing arguments to Unicode Peter Otten <__peter__@web.de> - 2015-10-07 00:55 +0200

csiph-web