diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-04-21 09:13:15 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-04-21 09:13:15 (GMT) |
commit | a3f98d6bac3fbfc0e010360b11793bbd8a8f4c85 (patch) | |
tree | 5486edd566e74a59ec25420ad07e79d92b0349d7 | |
parent | e99b97e58a274eb61d7346e786ad242e5253d3db (diff) | |
download | cpython-a3f98d6bac3fbfc0e010360b11793bbd8a8f4c85.zip cpython-a3f98d6bac3fbfc0e010360b11793bbd8a8f4c85.tar.gz cpython-a3f98d6bac3fbfc0e010360b11793bbd8a8f4c85.tar.bz2 |
Give UserDict new __contains__ and __iter__ methods.
-rw-r--r-- | Lib/UserDict.py | 4 | ||||
-rw-r--r-- | Lib/test/test_userdict.py | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/Lib/UserDict.py b/Lib/UserDict.py index b642db7..bbd4ca0 100644 --- a/Lib/UserDict.py +++ b/Lib/UserDict.py @@ -40,3 +40,7 @@ class UserDict: return self.data[key] def popitem(self): return self.data.popitem() + def __contains__(self, key): + return key in self.data + def __iter__(self): + return iter(self.data) diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py index 08b7a13..9aa01d0 100644 --- a/Lib/test/test_userdict.py +++ b/Lib/test/test_userdict.py @@ -81,12 +81,15 @@ verify(u2.keys() == d2.keys()) verify(u2.items() == d2.items()) verify(u2.values() == d2.values()) -# Test has_key +# Test has_key and "in". for i in u2.keys(): verify(u2.has_key(i) == 1) + verify((i in u2) == 1) verify(u1.has_key(i) == d1.has_key(i)) + verify((i in u1) == (i in d1)) verify(u0.has_key(i) == d0.has_key(i)) + verify((i in u0) == (i in d0)) # Test update @@ -100,3 +103,14 @@ for i in u2.keys(): verify(u2.get(i) == u2[i]) verify(u1.get(i) == d1.get(i)) verify(u0.get(i) == d0.get(i)) + +# Test "in" iteration. +for i in xrange(20): + u2[i] = str(i) +ikeys = [] +for k in u2: + ikeys.append(k) +ikeys.sort() +keys = u2.keys() +keys.sort() +verify(ikeys == keys) |