I am trying to understand a piece of code I am currently working through (its pacman).
I have the following 2D array in Python:
self.data = [[initialValue for y in range(height)] for x in range(width)]
The variables initialValue is default to F. Therefore the array is a list of lists containing the value 'F' for each element of the list (like so):
# F F F F
# F F F F
# F F F F
Now my code does the following - I do understand this part, but thought to include it in case it matters for my question:
def __getitem__(self, i):
return self.data[i]
def __setitem__(self, key, item):
self.data[key] = item
def __str__(self):
out = [[str(self.data[x][y])[0] for x in range(self.width)] for y in range(self.height)]
out.reverse() #reverses the oder of with the last row of the two dimensional array becoming the first row.
return '\n'.join([''.join(x) for x in out]) #every line is appended with a space '' followed by a line-break with '\n'
The latter turns the Grid into a chessboard of F's by using the \n demarcator. #F F F #F F F #F F F #F F F
def __eq__(self, other): # The __eq__ method compares other instances of the class with itself
if other == None: return False
return self.data == other.data
def __hash__(self):
# return hash(str(self))
base = 1
h = 0
for l in self.data:
for i in l:
if i:
h += base
base *= 2
return hash(h)
I happen to understand most of it, *but I get lost when it comes to the _hash_ function.*
I did research hashing and hash tables, but I can't seem to find what the function hash() does to our variable h?
What confuses me is the following:
1. It seems like we are creating some kind of hash table here based on self.data, but it seems like we leave this hash-table empty? Why would we do this?
2. It seems to me like the loop inside the _hash_ function is somehow linked to generating keys.
At this stage I can imagine that h is some form of key. The value of the key itself seems to increase every time it passes through the loop.
What confuses me is what the table would actually look like?
Is it this?
# 1 - value
# 2 - value
# 3 - value
# 6 - value
# 7 - value
# 8 - value
etc...
3. Can anybody point me to a resource that would provide me with an idea of what hash(h) does to h procedurally?
Thank you very much for reading this and for wanting to help out.
Mike
__hash__return hash(h)is just equivalent toreturn h...