{ "metadata": { "name": "", "signature": "sha256:58382d44df2c955998ebbf2d8117ea0b047479d698e13fa9ec4b201059d38042" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "\n", "class Circle:\n", " def __init__(self, x, y, r):\n", " self.x = x\n", " self.y = y\n", " self.r = r\n", "\n", " def area(self):\n", " return math.pi * self.r ** 2\n", "\n", "\n", "c = Circle(0, 0, 1)\n", "print(c.x)\n", "print(c.y)\n", "print(c.r)\n", "c.r = 2\n", "print(c.r)\n", "print(c.area())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "0\n", "1\n", "2\n", "12.566370614359172\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0421\u0432\u043e\u0439 \u0433\u0435\u0442\u0442\u0435\u0440 \u0438 \u0441\u0435\u0442\u0442\u0435\u0440" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "\n", "class Circle:\n", " def __init__(self, x, y, r):\n", " self.x = x\n", " self.y = y\n", " self.set_r(r)\n", "\n", " def area(self):\n", " return math.pi * self.r ** 2\n", "\n", " def set_r(self, r):\n", " if r < 0:\n", " raise ValueError(\"Radius should be nonnegative\")\n", " self._r = r\n", "\n", " def get_r(self):\n", " return self._r\n", "\n", "\n", "c = Circle(0, 0, 1)\n", "print(c.x)\n", "print(c.y)\n", "c.set_r(2)\n", "print(c.get_r())\n", "c.set_r(-1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "0\n", "2\n" ] }, { "ename": "ValueError", "evalue": "Radius should be nonnegative", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_r\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_r\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 27\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_r\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mset_r\u001b[0;34m(self, r)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mset_r\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Radius should be nonnegative\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_r\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: Radius should be nonnegative" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 @property" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "\n", "class Circle:\n", " def __init__(self, x, y, r):\n", " self.x = x\n", " self.y = y\n", " self.r = r\n", "\n", " def area(self):\n", " return math.pi * self.r ** 2\n", "\n", " @property\n", " def r(self):\n", " return self._r\n", "\n", " @r.setter\n", " def r(self, r):\n", " if r < 0:\n", " raise ValueError(\"Radius should be nonnegative\")\n", " self._r = r\n", "\n", "\n", "c = Circle(0, 0, 1)\n", "print(c.x)\n", "print(c.y)\n", "c.r = 2\n", "print(c.r)\n", "c.r = -1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "0\n", "2\n" ] }, { "ename": "ValueError", "evalue": "Radius should be nonnegative", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 29\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mr\u001b[0;34m(self, r)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Radius should be nonnegative\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_r\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: Radius should be nonnegative" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0421\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \\_\\_\u043c\u0435\u0442\u043e\u0434\u044b\\_\\_" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "\n", "class Circle:\n", " def __init__(self, x, y, r):\n", " self.x = x\n", " self.y = y\n", " self.r = r\n", "\n", " def area(self):\n", " return math.pi * self.r ** 2\n", "\n", " @property\n", " def r(self):\n", " return self._r\n", "\n", " @r.setter\n", " def r(self, r):\n", " if r < 0:\n", " raise ValueError(\"Radius should be nonnegative\")\n", " self._r = r\n", "\n", " def __str__(self):\n", " return 'Circle: x={}, y={}, r={}'.format(self.x, self.y, self.r)\n", "\n", " def __repr__(self):\n", " return 'Circle(x={}, y={}, r={})'.format(self.x, self.y, self.r)\n", "\n", " def __lt__(self, other):\n", " return self.r < other.r\n", "\n", " def __add__(self, other):\n", " return Circle(self.x + other.x, self.y + other.y, self.r + other.r)\n", "\n", "\n", "c = Circle(0, 0, 1)\n", "print(c)\n", "print(repr(c))\n", "c2 = Circle(x=0, y=0, r=1)\n", "print(c2)\n", "print(c < c2)\n", "print(c + c2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Circle: x=0, y=0, r=1\n", "Circle(x=0, y=0, r=1)\n", "Circle: x=0, y=0, r=1\n", "False\n", "Circle: x=0, y=0, r=2\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }