From c226c311392abbda437300eaa9841d9e839854c0 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 23 Jan 2008 00:04:40 +0000 Subject: Let pprint() support sets and frozensets (suggested by David Mertz). --- Doc/library/pprint.rst | 3 +++ Lib/pprint.py | 17 +++++++++++++++-- Misc/NEWS | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index d073714..ae9677f 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -25,6 +25,9 @@ width constraint. dictionary was sorted only if its display required more than one line, although that wasn't documented. +.. versionchanged:: 2.6 + Added support for :class:`set` and :class:`frozenset`. + The :mod:`pprint` module defines one class: .. First the implementation class: diff --git a/Lib/pprint.py b/Lib/pprint.py index 89f99d2..9359de3 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -162,11 +162,24 @@ class PrettyPrinter: write('}') return - if (issubclass(typ, list) and r is list.__repr__) or \ - (issubclass(typ, tuple) and r is tuple.__repr__): + if ((issubclass(typ, list) and r is list.__repr__) or + (issubclass(typ, tuple) and r is tuple.__repr__) or + (issubclass(typ, set) and r is set.__repr__) or + (issubclass(typ, frozenset) and r is frozenset.__repr__) + ): if issubclass(typ, list): write('[') endchar = ']' + elif issubclass(typ, set): + write('set([') + endchar = '])' + object = sorted(object) + indent += 4 + elif issubclass(typ, frozenset): + write('frozenset([') + endchar = '])' + object = sorted(object) + indent += 9 else: write('(') endchar = ')' diff --git a/Misc/NEWS b/Misc/NEWS index 3b55d74..103a353 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -378,6 +378,8 @@ Core and builtins Library ------- +- The pprint module now supports sets and frozensets. + - #1221598: add optional callbacks to ftplib.FTP's storbinary() and storlines() methods. (Contributed by Phil Schwartz) -- cgit v0.12