Skip to content

Instantly share code, notes, and snippets.

@mahmoud
Created June 24, 2011 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmoud/1044311 to your computer and use it in GitHub Desktop.
Save mahmoud/1044311 to your computer and use it in GitHub Desktop.
python dict destruction
import random
PyDict_MINSIZE = 8 # from dictobject.h
class DictDestroyer(object):
def __init__(self):
self.hash = 1
def __hash__(self):
self.hash = random.randint(0, 2**31)
return self.hash
def __eq__(self, y):
print 'hi'
return id(self)==id(y)
def test():
d = DictDestroyer()
a = {d:1, d:2, d:3} # less than PyDict_MINSIZE
for i in range(1,15):
try:
x = a[d]
print i, '-', x, d.hash, d.hash % PyDict_MINSIZE
except:
pass
# Question: Is there a way to tell the size of a dictionary or when it resizes?
=======
# iteration - value hash modulo
>>> dict_fun.test()
2 - 2 1001055789 5
4 - 2 692789333 5
7 - 2 364101293 5
9 - 3 1281032169 1
10 - 3 1754057753 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment