{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cbee6759",
   "metadata": {},
   "source": [
    "# 14-22 · Практика: __str__ и __eq__\n",
    "\n",
    "Практика к разделу [«Практика: применяем __str__ и __eq__»](../../site/chapters/glava-14/14-22-primenyaem-dunder-metody.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2df5192a",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Применить __str__ и __eq__ к классу Tochka."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f16009",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f6e20d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Tochka:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"({self.x}, {self.y})\"\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return self.x == other.x and self.y == other.y\n",
    "\n",
    "a = Tochka(1, 2)\n",
    "b = Tochka(1, 2)\n",
    "c = Tochka(5, 5)\n",
    "print(str(a), a == b, a == c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "043f38c3",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача\n",
    "\n",
    "Добавьте `Tochka` метод `rasstoyanie_do(other)`, возвращающий евклидово расстояние до другой точки."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d432ae09",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Tochka:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"({self.x}, {self.y})\"\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return self.x == other.x and self.y == other.y\n",
    "\n",
    "    def rasstoyanie_do(self, other):\n",
    "        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5\n",
    "\n",
    "nachalo = Tochka(0, 0)\n",
    "konec = Tochka(3, 4)\n",
    "rasstoyanie = nachalo.rasstoyanie_do(konec)\n",
    "print(rasstoyanie)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "name": "python",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
