Skip to content

Commit 255147c

Browse files
committed
Added more readable __str__ and __repr__ methods to MergeDict.
Thanks, [email protected]. Fixed #3508. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13721 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent dee7ef8 commit 255147c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ answer newbie questions, and generally made Django that much better:
238238
239239
jdetaeye
240240
241+
241242
Zak Johnson <[email protected]>
242243
Nis Jørgensen <[email protected]>
243244
Michael Josephson <http://www.sdjournal.com/>

django/utils/datastructures.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ def copy(self):
7777
"""Returns a copy of this object."""
7878
return self.__copy__()
7979

80+
def __str__(self):
81+
'''
82+
Returns something like
83+
84+
"{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}"
85+
86+
instead of the generic "<object meta-data>" inherited from object.
87+
'''
88+
return str(dict(self.items()))
89+
90+
def __repr__(self):
91+
'''
92+
Returns something like
93+
94+
MergeDict({'key1': 'val1', 'key2': 'val2'}, {'key3': 'val3'})
95+
96+
instead of generic "<object meta-data>" inherited from object.
97+
'''
98+
dictreprs = ', '.join(repr(d) for d in self.dicts)
99+
return '%s(%s)' % (self.__class__.__name__, dictreprs)
100+
80101
class SortedDict(dict):
81102
"""
82103
A dictionary that keeps its keys in the order in which they're inserted.

0 commit comments

Comments
 (0)