Ask Your Question
2

Could anyone explain this unexpected behaviour of the interface?

asked 2018-06-05 17:48:02 +0100

zivaoupas gravatar image

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]
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
4

answered 2018-06-05 20:17:09 +0100

slelievre gravatar image

updated 2018-06-06 07:14:06 +0100

The 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:

and there is a Sage Trac ticket about this

edit flag offensive delete link more
2

answered 2018-06-05 22:16:23 +0100

Sébastien gravatar image

updated 2018-06-05 22:23:21 +0100

As answered here, 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?)

edit flag offensive delete link more

Comments

Thanks @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).

slelievre gravatar imageslelievre ( 2018-06-06 07:15:57 +0100 )edit
2

answered 2018-06-06 17:57:58 +0100

Nicolas M. Thiéry gravatar image

See also this IPython issue: https://github.com/ipython/ipython/is...

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 ]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-06-05 17:48:02 +0100

Seen: 6,634 times

Last updated: Jun 06 '18