mirror of
https://github.com/python/cpython.git
synced 2024-12-12 19:33:52 +08:00
1697b9cdf6
The constructor now takes an optional dictionary. Use isinstance() where appropriate.
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
# 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 __repr__(self): return repr(self.data)
|
|
def __cmp__(self, dict):
|
|
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): 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, 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 dict.items():
|
|
self.data[k] = v
|
|
def get(self, key, failobj=None):
|
|
return self.data.get(key, failobj)
|