[PYTHON MATRIX-SIG] Re: Printing arrays and repr vs. str

Guido van Rossum guido@CNRI.Reston.Va.US
Fri, 28 Feb 1997 14:57:32 -0500


> Under the current design, str(a) and repr(a) both behave "properly" for 
> python's design.  Unfortunately, the print method uses the str(a) 
> representation, which according to Guido is  not the correct way to display 
> python objects (and if anybody should know it's Guido ;-).

I'm sorry that it's so complicated, but this is still not quite
right.  (I had to browse the sources myself, ehm.)

When an object is being "printed" to a "real file", its print method
is invoked with a flag.  The flag can be 0 or Py_PRINT_RAW.  When it
is 0, you are supposed to emulate repr(); when it is Py_PRINT_RAW, you
are supposed to emulate str().  For some details, see print_object()
in Objects/object.c.  (Actually, it's a word of flag bits.)

When an object is being "printed" to something that only emulates a
file, Python calls repr() or str() directly.  This also happens when
the object does not have a print method.

The print statement uses Py_PRINT_RAW.  Expressions entered at the >>>
prompt use a 0 flag.

> Does anybody have any good suggestions on this one?

Have a user preference flag that determines whether repr() should
behave like str()?  Set it to on by default when Python is used
interactively.

--Guido van Rossum (home page: http://www.python.org/~guido/)

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________