+ def __hash__(self):
+ return hash(tuple(self.coordinates()))
+
+ def __repr__(self):
+ string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
+ for symbol, coordinate in self.coordinates()])
+ return '{}({{{}}})'.format(self.__class__.__name__, string)
+
+ def _map(self, func):
+ for symbol, coordinate in self.coordinates():
+ yield symbol, func(coordinate)
+
+ def _iter2(self, other):
+ if self.symbols != other.symbols:
+ raise ValueError('arguments must belong to the same space')
+ coordinates1 = self._coordinates.values()
+ coordinates2 = other._coordinates.values()
+ yield from zip(self.symbols, coordinates1, coordinates2)
+
+ def _map2(self, other, func):
+ for symbol, coordinate1, coordinate2 in self._iter2(other):
+ yield symbol, func(coordinate1, coordinate2)
+
+
+class Point(Coordinates):
+ """
+ This class represents points in space.
+ """
+
+ def __new__(cls, coordinates=None):
+ if isinstance(coordinates, Mapping):
+ coordinates = coordinates.items()
+ self = object().__new__(cls)
+ self._coordinates = OrderedDict()
+ for symbol, coordinate in sorted(coordinates,
+ key=lambda item: item[0].sortkey()):
+ if not isinstance(symbol, Symbol):
+ raise TypeError('symbols must be Symbol instances')
+ if not isinstance(coordinate, numbers.Real):
+ raise TypeError('coordinates must be real numbers')
+ self._coordinates[symbol] = coordinate
+ return self
+
+ def isorigin(self):
+ return not bool(self)
+