How can py tuple implicit cast to int?

63 views Asked by At

I have some code that for a grid of ints requires comparing cols with rows. The grid dimension is unknown at compile time.

So I need a good hash function for a 1-d array of int.

The following code compiles and runs as expected as it uniquely identifies any row or col :

def hashValue(a: List[int]) -> int:
    return tuple(a)

rows = {}
for i in range(len(grid)):
    hash = hashValue(grid[i])
    if hash in rows:
        rows[hash] += 1
    else:
        rows.append(hash,1)    

But I don't understand what's going on under the hood.

The hash function creates a tuple from the List[int] and returns an int where it properly serves as a key in the dictionary of rows with which cols are compared later on.

But how is py converting the tuple to an int ?

Does py maintain an internal hash of the immutable tuple ?

0

There are 0 answers