From bcb6ca4d5e926d9571d150fb045c5ac4b53f8ecd Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Jun 2014 12:53:44 +0100 Subject: py: Implement full behaviour of dict.update(), and dict(). Add keyword args to dict.update(), and ability to take a dictionary as argument. dict() class constructor can now use dict.update() directly. This patch loses fast path for dict(other_dict), but is that really needed? Any anyway, this idiom will now re-hash the dictionary, so is arguably more memory efficient. Addresses issue #647. --- tests/basics/dict_construct.py | 16 ++++++++++++++++ tests/basics/dict_update.py | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/basics/dict_construct.py (limited to 'tests/basics') diff --git a/tests/basics/dict_construct.py b/tests/basics/dict_construct.py new file mode 100644 index 000000000..0035e9c0f --- /dev/null +++ b/tests/basics/dict_construct.py @@ -0,0 +1,16 @@ +# dict constructor + +d = dict() +print(d) + +d = dict({1:2}) +print(d) + +d = dict(a=1) +print(d) + +d = dict({1:2}, a=3) +print(d[1], d['a']) + +d = dict([(1, 2)], a=3, b=4) +print(d[1], d['a'], d['b']) diff --git a/tests/basics/dict_update.py b/tests/basics/dict_update.py index 46d1f41b5..ab1a63304 100644 --- a/tests/basics/dict_update.py +++ b/tests/basics/dict_update.py @@ -8,3 +8,9 @@ print(len(d)) d.update([(1,4)]) print(d[1]) print(len(d)) + +# using keywords +d.update(a=5) +print(d['a']) +d.update([(1,5)], b=6) +print(d[1], d['b']) -- cgit v1.2.3