summaryrefslogtreecommitdiffstats
path: root/Lib/UserDict.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2002-11-27 08:29:11 (GMT)
committerRaymond Hettinger <python@rcn.com>2002-11-27 08:29:11 (GMT)
commite4827eb2a2c9eed8911a6874866a224972ff40ab (patch)
tree5195b5c1ede95d2a8251247a4a52598aaa7ec510 /Lib/UserDict.py
parente33d3df03079d704edbe159be079dc387058f044 (diff)
downloadcpython-e4827eb2a2c9eed8911a6874866a224972ff40ab.zip
cpython-e4827eb2a2c9eed8911a6874866a224972ff40ab.tar.gz
cpython-e4827eb2a2c9eed8911a6874866a224972ff40ab.tar.bz2
Bring UserDict in-sync with changes to dict.
Constructor accepts optional keyword arguments after a optional items list. Add fromkeys() as an alternate constructor from an iterable over keys. Expand related unittests.
Diffstat (limited to 'Lib/UserDict.py')
-rw-r--r--Lib/UserDict.py18
1 files changed, 12 insertions, 6 deletions
diff --git a/Lib/UserDict.py b/Lib/UserDict.py
index 02ddfc4..34ceb3a 100644
--- a/Lib/UserDict.py
+++ b/Lib/UserDict.py
@@ -2,13 +2,13 @@
class UserDict:
def __init__(self, dict=None, **kwargs):
- self.data = kwargs # defaults to {}
+ self.data = {}
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))
+ if not hasattr(dict,'keys'):
+ dict = type({})(dict) # make mapping from a sequence
+ self.update(dict)
+ if len(kwargs):
+ self.update(kwargs)
def __repr__(self): return repr(self.data)
def __cmp__(self, dict):
if isinstance(dict, UserDict):
@@ -61,6 +61,12 @@ class UserDict:
return self.data.popitem()
def __contains__(self, key):
return key in self.data
+ def fromkeys(cls, iterable, value=None):
+ d = cls()
+ for key in iterable:
+ d[key] = value
+ return d
+ fromkeys = classmethod(fromkeys)
class IterableUserDict(UserDict):
def __iter__(self):