Give UserDict new __contains__ and __iter__ methods.

This commit is contained in:
Tim Peters 2001-04-21 09:13:15 +00:00
parent e99b97e58a
commit a3f98d6bac
2 changed files with 19 additions and 1 deletions

View File

@ -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)

View File

@ -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)