ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Jun 2018 10:57:58 -0500- Could anyone explain this unexpected behaviour of the interface?http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/
Input
w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
w
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
Input
w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
w
Output
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
Tue, 05 Jun 2018 10:48:02 -0500http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/
- Answer by Nicolas M. Thiéry for <p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
w
</code></pre>
<p>Output</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
</code></pre>
<p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
w
</code></pre>
<p>Output</p>
<pre><code>[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
</code></pre>
http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42533#post-id-42533See also this IPython issue: https://github.com/ipython/ipython/issues/8741
One way to work around is to use ascii art output:
sage: %display unicode_art
sage: list(range(100))
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 ]Wed, 06 Jun 2018 10:57:58 -0500http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42533#post-id-42533
- Answer by Sébastien for <p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
w
</code></pre>
<p>Output</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
</code></pre>
<p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
w
</code></pre>
<p>Output</p>
<pre><code>[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
</code></pre>
http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42518#post-id-42518As [answered here](https://stackoverflow.com/questions/19010036/how-to-make-ipython-output-a-list-without-line-breaks-after-elements), it should be possible to turn off the pretty printing as in IPython:
$ sage -ipython
Python 2.7.15 (default, May 20 2018, 23:45:14)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: range(23)
Out[1]:
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
In [2]: %pprint
Pretty printing has been turned OFF
In [3]: range(23)
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
but unfortunately, it does not work in Sage (bug?)Tue, 05 Jun 2018 15:16:23 -0500http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42518#post-id-42518
- Comment by slelievre for <p>As <a href="https://stackoverflow.com/questions/19010036/how-to-make-ipython-output-a-list-without-line-breaks-after-elements">answered here</a>, it should be possible to turn off the pretty printing as in IPython:</p>
<pre><code>$ sage -ipython
Python 2.7.15 (default, May 20 2018, 23:45:14)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: range(23)
Out[1]:
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
In [2]: %pprint
Pretty printing has been turned OFF
In [3]: range(23)
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
</code></pre>
<p>but unfortunately, it does not work in Sage (bug?)</p>
http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?comment=42525#post-id-42525Thanks @Sébastien for the extra information and pointers. I was really hoping someone would dig further than my brief "workaround" answer. Digging even a little further, I located a Sage Trac ticket for this bug (see my edited answer).Wed, 06 Jun 2018 00:15:57 -0500http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?comment=42525#post-id-42525
- Answer by slelievre for <p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
w
</code></pre>
<p>Output</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
</code></pre>
<p>Input</p>
<pre><code>w = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
w
</code></pre>
<p>Output</p>
<pre><code>[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22]
</code></pre>
http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42512#post-id-42512The display of lists seems to get extra newline characters when they go
over a certain number of elements.
But you can get a unified behaviour by using `print`, `str` or `repr`.
sage: a = list(range(22))
sage: b = list(range(23))
sage: print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
sage: print(b)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
sage: repr(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: repr(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'
sage: str(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: str(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'
sage: a.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'
sage: a.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'
**Edit** (after reading @Sébastien's answer and the stackoverflow answer
mentioned in it). Note that what makes the pretty printing of the list get extra
newline characters is not the length of the list, but whether its string
representation goes over 79 characters.
sage: c = list(range(12345678901234, 12345678901239))
sage: len(c)
5
sage: len(str(c))
80
sage: c
[12345678901234,
12345678901235,
12345678901236,
12345678901237,
12345678901238]
sage: print(c)
[12345678901234, 12345678901235, 12345678901236, 12345678901237, 12345678901238]
Note that this was asked before on Ask Sage:
- [Ask Sage question #37477: long list goes vertical](https://ask.sagemath.org/question/37477/if-i-return-a-list-from-a-function-that-is-more-than-26-elements-the-list-goes-vertical/)
and there is a Sage Trac ticket about this
- [Sage Trac ticket #22968: Sage does not handle %pprint correctly](https://trac.sagemath.org/ticket/22968)Tue, 05 Jun 2018 13:17:09 -0500http://ask.sagemath.org/question/42509/could-anyone-explain-this-unexpected-behaviour-of-the-interface/?answer=42512#post-id-42512