Created
January 23, 2013 21:43
-
-
Save atiaxi/4613940 to your computer and use it in GitHub Desktop.
iteritems behaves strangely!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import MutableMapping | |
class Itertest(MutableMapping): | |
def __init__(self): | |
self.underlying = {} | |
def __getitem__(self, key): | |
print "Getting %s" % key | |
return self.underlying[key] | |
def __setitem__(self, key, value): | |
print "Setting %s to %s" % (key, value) | |
self.underlying[key]=value | |
def __delitem__(self, key): | |
del self.underlying[key] | |
def __len__(self): | |
return len(self.underlying) | |
def __iter__(self): | |
return iter(self.underlying) | |
x = Itertest() | |
x['a'] = 3 | |
x['b'] = 4 | |
x['k'] = 'cd' | |
print "Regular iteration" | |
for key in x: | |
print key | |
print 'iteritems:' | |
for key, val in x.iteritems(): | |
print key, val | |
print 'items:' | |
for key, val in x.items(): | |
print key, val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment