Skip to content

Instantly share code, notes, and snippets.

@atiaxi
Created January 23, 2013 21:43
Show Gist options
  • Save atiaxi/4613940 to your computer and use it in GitHub Desktop.
Save atiaxi/4613940 to your computer and use it in GitHub Desktop.
iteritems behaves strangely!
#!/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