[Python-Dev] pprint and list/tuple/dict subclasses

Fred L. Drake, Jr. fdrake at acm.org
Tue Dec 2 14:18:54 EST 2003


Hunter Peress writes:
 > I see the arguments,but the following code works fine. so what
 > exactly is the issue?

The code doesn't work when the repr of the subclass instance should be
wrapped:

 > #!/usr/bin/python
 > import sys,commands,os,re,string
 > from pprint import pprint as ppr
 > 
 > class t(list):
 >   pass
 > 
 > inst = t()
 > inst.append(1)
 > inst.append({2:3})
 > inst.append([4,5,6,[7,8]])

Change the last line to:

for i in range(10):
    inst.append(range(10))

and what you get is:

[1, {2: 3}, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7,
8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1,
2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3,
4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

(wrapped by pasting into my mail client), but what's expected is:

[1,
 {2: 3},
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation



More information about the Python-Dev mailing list