# Example 10-12. Part of vector_v4.py: two imports and __hash__ method added to Vector class from vector_v3.py


import functools
import operator
import array


class Vector:
    typecode = 'd'

    def __init__(self, components):
        self._components = array(self.typecode, components)

    # many lines omitted in book listing...
    def __eq__(self, other):
        return tuple(self) == tuple(other)

    def __hash__(self):
        hashes = (hash(x) for x in self._components)  #
        return functools.reduce(operator.xor, hashes, 0)
