[PYTHON MATRIX-SIG] The difference between repr and str, and printing arrays
Jim Hugunin
hugunin@mit.edu
Thu, 27 Feb 1997 15:30:52 -0500
I think I just fixed a bug in the way that PythonWin prints out objects in
its interpreter. The cross-posting is to make sure I not instead confused
about the "proper" way for things to work. Here's the issue (this uses the
Numeric extensions to python, but should be easy enough to follow for
anyone interested in repr's and str's). Under PythonWin's interpreter I do
the following:
>>> from Numeric import *
>>> a = array([1,2,3])
>>> a
array([1, 2, 3], 'l')
>>> repr(a)
"array([1, 2, 3], 'l')"
>>> str(a)
'1 2 3'
Notice that when I just type "a", I'm getting its "repr", rather than the
printable string version (which is what I want).
Under most other interpreters (DOS python for example) I get:
>>> from Numeric import *
>>> a = array([1,2,3])
>>> a
1 2 3
>>> repr(a)
"array([1, 2, 3], 'l')"
>>> str(a)
'1 2 3'
This is the behavior I'd hoped for (and designed the repr/str conventions
of NumPy around).
The discrepancy is based on the fact that PythonWin prints out the
repr(eval(command)) instead of either the str(eval(command)) or just
eval(command) when something is typed to it's interactive prompt.
I can fix PythonWin by changing line 317 of interact.py to read "print ret"
or "print str(ret)" instead of "print repr(ret)".
When I fix it, I get the following:
>>> from Numeric import *
>>> a = arange(10)
>>> a
0 1 2 3 4 5 6 7 8 9
>>> repr(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'l')
>>> str(a)
0 1 2 3 4 5 6 7 8 9
Notice that the arrays are now printing as I would wish, but that the
quotes have been removed from around the strings in the results. I happen
to like this appearence in general, but I wanted to understand two things:
1) Why does there not seem to be a simple way to get both strings and
arrays to print out the way they do in most command-line interpreters?
2) Should this fix be applied to PythonWin? Does it do the right thing,
and would it break anything?
Thanks for any comments - Jim
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________