Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. # Пример того, как можно делать запись вектороного произведения точек в виде cp[v, w], а не cp(v, w)
  2. class Point:
  3. """Класс Точка"""
  4. __slots__ = ['x', 'y']
  5. def __init__(self, x=0, y=0):
  6. self.x = x
  7. self.y = y
  8. def __sub__(v, w):
  9. return Point(v.x - w.x, v.y - w.y)
  10. def __repr__(self):
  11. return f'Point({self.x}, {self.y})'
  12. def __str__(self):
  13. return f'({self.x}, {self.y})'
  14.  
  15. def dp(v, w):
  16. """Dot product — скалярное произведение"""
  17. return v.x * w.x + v.y * w.y
  18.  
  19. class _CrossProductHelper:
  20. """Специальный класс, который позволит обрабатывать cp[key]"""
  21. @staticmethod # Ибо самого объекта класса никому не нужно
  22. def __getitem__(key): # key в данном случае — это кортеж из пары точек
  23. v, w = key
  24. return v.x * w.x - v.y * w.y
  25. cp = _CrossProductHelper()
  26.  
  27.  
  28. # Берём две точки
  29. v = Point(1, 1)
  30. w = Point(1, -1)
  31.  
  32. print('Dot (scalar) product of', v, 'and', w, 'is', dp(v, w))
  33. print('Cross product of', v, 'and', w, 'is', cp[v, w])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement