mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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.
This commit is contained in:
parent
ba2cf078d2
commit
54405456e5
2 changed files with 11 additions and 3 deletions
|
@ -1,9 +1,14 @@
|
|||
"""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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue