6

Can I have pretty printed data output as in pprint.pprint (new lines, indentation), but also shortened lists as in reprlib.repr at the same time?

An ugly hack seems to be pprint(eval(reprlib.repr(data))), but is there a better way?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Gere
  • 12,075
  • 18
  • 62
  • 94
  • why don't you just crop the list yourself? just reference `list[0:max]` and it will shorten it (to clarify: even for lists of less than maximum length). You can even be fancy and `append(...)` at the end. – Scelesto May 18 '15 at 13:39
  • @Scelesto `0` is the default slice start, so `list[:max]` works too. – jonrsharpe May 18 '15 at 13:46
  • @d33tah "repr is a Python built-in function that returns the canonical string representation of its input" The question isn't about it. – vaultah May 18 '15 at 14:12
  • @Scelesto That only works if the top level is the long list, not if the object *contains* long lists. – RoadieRich May 18 '15 at 14:42

1 Answers1

-2

You can change the way an object is printed by overriding the __repr__() method on its class.

Python will allow you to override the repr of any class, so use it with care.

sorin
  • 161,544
  • 178
  • 535
  • 806