summaryrefslogtreecommitdiffstats
path: root/Lib/UserDict.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-03-26 15:31:12 (GMT)
committerGuido van Rossum <guido@python.org>1999-03-26 15:31:12 (GMT)
commit1697b9cdf6b799cbe23c18396dbab3baa8056ce1 (patch)
treebaa025d5388646fd3cf42b5726a6f538ef1b60fe /Lib/UserDict.py
parentaa3828aa354e1f50a8d073c40c41027c2bd43739 (diff)
downloadcpython-1697b9cdf6b799cbe23c18396dbab3baa8056ce1.zip
cpython-1697b9cdf6b799cbe23c18396dbab3baa8056ce1.tar.gz
cpython-1697b9cdf6b799cbe23c18396dbab3baa8056ce1.tar.bz2
Improved a bunch of things.
The constructor now takes an optional dictionary. Use isinstance() where appropriate.
Diffstat (limited to 'Lib/UserDict.py')
-rw-r--r--Lib/UserDict.py29
1 files changed, 16 insertions, 13 deletions
diff --git a/Lib/UserDict.py b/Lib/UserDict.py
index 08f3161..50fee89 100644
--- a/Lib/UserDict.py
+++ b/Lib/UserDict.py
@@ -1,33 +1,36 @@
# A more or less complete user-defined wrapper around dictionary objects
class UserDict:
- def __init__(self): self.data = {}
+ def __init__(self, dict=None):
+ self.data = {}
+ if dict is not None: self.update(dict)
def __repr__(self): return repr(self.data)
def __cmp__(self, dict):
- if type(dict) == type(self.data):
- return cmp(self.data, dict)
- else:
+ if isinstance(dict, UserDict):
return cmp(self.data, dict.data)
+ else:
+ return cmp(self.data, dict)
def __len__(self): return len(self.data)
def __getitem__(self, key): return self.data[key]
def __setitem__(self, key, item): self.data[key] = item
def __delitem__(self, key): del self.data[key]
- def clear(self): return self.data.clear()
+ def clear(self): self.data.clear()
def copy(self):
+ if self.__class__ is UserDict:
+ return UserDict(self.data)
import copy
return copy.copy(self)
def keys(self): return self.data.keys()
def items(self): return self.data.items()
def values(self): return self.data.values()
def has_key(self, key): return self.data.has_key(key)
- def update(self, other):
- if type(other) is type(self.data):
- self.data.update(other)
+ def update(self, dict):
+ if isinstance(dict, UserDict):
+ self.data.update(dict.data)
+ elif isinstance(dict, type(self.data)):
+ self.data.update(dict)
else:
- for k, v in other.items():
+ for k, v in dict.items():
self.data[k] = v
def get(self, key, failobj=None):
- if self.data.has_key(key):
- return self.data[key]
- else:
- return failobj
+ return self.data.get(key, failobj)