Can I make unicode in a repr() print readably?

"Martin v. Löwis" martin at
Sun Sep 10 10:52:28 CEST 2006

Terry Hancock schrieb:
> Is it possible to define some combination of __repr__, __str__,
> and/or __unicode__ so that the unicode() wrapper isn't necessary
> in this statement:

I'm not aware of a way of doing so.

> Or, put another way, what exactly does 'print' do when it gets
> a class instance to print? It seems to do the right thing if
> given a unicode or string object, but I cant' figure out how to
> make it do the same thing for a class instance.

It won't. PyFile_WriteObject checks for Unicode objects, and whether
the file has an encoding attribute set, and if so, encodes the
Unicode object.

If it is not a Unicode object, it falls through to PyObject_Print,
which first checks for the tp_print slot (which can't be set in
Python), then uses PyObject_Str (which requires that the __str__
result is a true byte string), or PyObject_Repr (if the RAW
flag isn't set - it is when printing). PyObject_Str first checks
for tp_str; if that isn't set, it falls back to PyObject_Repr.

> And I understand that I might want that if I'm working in
> an ASCII-only terminal.  But it's a big help to be able to
> read/recognize the labels when I'm working with localized
> encodings, and I'd like to save the extra typing if I'm
> going to be looking at a lot of these

You can save some typing, of course, with a helper function:

def p(o):
  print unicode(o)

I agree that this is not optimal; contributions are welcome.
It would probably be easiest to drop the guarantee that
PyObject_Str returns a true string, or use _PyObject_Str
(which does not make this guarantee) in PyObject_Print.
One would have to think what the effect on backwards
compatibility is of such a change.


More information about the Python-list mailing list