Commit 54405456 authored by Raymond Hettinger's avatar Raymond Hettinger

Implement dict() style constructor.

Already supported dict() and dict(mapping).
Now supports dict(itemsequence) and
Just van Rossum's new syntax for dict(keywordargs).

Also, added related unittests.

The docs already promise dict-like behavior
so no update is needed there.
parent ba2cf078
"""A more or less complete user-defined wrapper around dictionary objects."""
class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)
def __init__(self, dict=None, **kwargs):
self.data = kwargs # defaults to {}
if dict is not None:
if hasattr(dict,'keys'): # handle mapping (possibly a UserDict)
self.update(dict)
else: # handle sequence
DICT = type({}) # because builtin dict is locally overridden
self.data.update(DICT(dict))
def __repr__(self): return repr(self.data)
def __cmp__(self, dict):
if isinstance(dict, UserDict):
......
......@@ -19,6 +19,9 @@ uu0 = UserDict(u0)
uu1 = UserDict(u1)
uu2 = UserDict(u2)
verify(UserDict(one=1, two=2) == d2) # keyword arg constructor
verify(UserDict([('one',1), ('two',2)]) == d2) # item sequence constructor
# Test __repr__
verify(str(u0) == str(d0))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment