1
0
Fork 0
Python2017/labs04/Klasy.ipynb

692 lines
20 KiB
Plaintext
Raw Permalink Normal View History

2017-12-03 11:08:00 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2017-12-03 13:05:05 +01:00
"# Wprowadzenie do Pythona: Klasy\n",
2017-12-03 11:08:00 +01:00
"\n",
"## Tomasz Dwojak\n",
"\n",
2017-12-03 13:05:05 +01:00
"### 3 grudnia 2017"
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Plan na dziś:\n",
" * klasy,\n",
2017-12-03 13:05:05 +01:00
" * wyjątki."
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Python jest językiem obiektowym \n",
" * Wszystko jest obiektem: liczby, napisy, None, funkcje, moduły (biblioteki)..."
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 1,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"0\nPython da się lubić !\n"
2017-12-03 11:08:00 +01:00
]
}
],
"source": [
"print((2017).imag)\n",
"print(' '.join(['Python', 'da', 'się', 'lubić', '!']))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Konstrukcja"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 6,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"<type 'instance'>\n<type 'instance'>\n"
2017-12-03 11:08:00 +01:00
]
}
],
"source": [
"class NajprostszaKlasa:\n",
" pass\n",
"\n",
"nasza_klasa = NajprostszaKlasa() # Uwaga na nawiasy na końcu!\n",
2017-12-14 12:28:39 +01:00
"print(type(nasza_klasa))\n",
"\n",
"nasza_klasa = NajprostszaKlasa # Uwaga na nawiasy na końcu!\n",
"print(type(nasza_klasa()))"
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## (Pseudo) Konstruktor \n",
"\n"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 14,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"2\n<__main__.Punkt instance at 0x00000000049B3CC8>\n"
2017-12-03 11:08:00 +01:00
]
}
],
"source": [
"class Punkt:\n",
2017-12-14 12:28:39 +01:00
" def __init__(self, x, y=0):\n",
2017-12-03 11:08:00 +01:00
" self.x = x\n",
" self.y = y\n",
2017-12-14 12:28:39 +01:00
" \n",
"# self jak this w C\n",
2017-12-03 11:08:00 +01:00
"\n",
2017-12-14 12:28:39 +01:00
"punkt = Punkt(2)\n",
"print(punkt.x)\n",
"print(punkt)"
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 20,
2017-12-03 11:08:00 +01:00
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Figura:\n",
" def __init__(self, vertexes):\n",
" self.vertexes = vertexes\n",
" \n",
" def liczba_wierzcholkow(self):\n",
" return len(self.vertexes)\n",
" \n",
2017-12-14 12:28:39 +01:00
" #metoda - jedynym argumentem jest self\n",
" #funkcja - \n",
" \n",
2017-12-03 11:08:00 +01:00
" def dodaj_wierzcholek(self, x):\n",
" self.vertexes.append(x)"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 22,
2017-12-03 11:08:00 +01:00
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2017-12-14 12:28:39 +01:00
"#dziedziczenie\n",
"\n",
2017-12-03 11:08:00 +01:00
"class Prostokat(Figura):\n",
" def __init__(self, vertexes):\n",
" super().__init__(vertexes)\n",
2017-12-14 12:28:39 +01:00
" #lub self.vertexes = vertexes\n",
2017-12-03 11:08:00 +01:00
" def czy_jestem_kwadratem(self):\n",
" pass\n",
" "
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 41,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
2017-12-14 12:28:39 +01:00
"execution_count": 41,
2017-12-03 11:08:00 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Figura:\n",
" def __init__(self, vertexes):\n",
" self.vertexes = vertexes\n",
" \n",
" def liczba_wierzcholkow(self):\n",
" return len(self.vertexes)\n",
" \n",
" def __len__(self):\n",
" return self.liczba_wierzcholkow()\n",
" \n",
2017-12-14 12:28:39 +01:00
"len(Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]))\n",
"o = Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)])\n",
"len(o)\n",
"o.__len__()\n",
"o.liczba_wierzcholkow()"
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Dobre praktyki: komentarze"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 33,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
2017-12-03 13:05:05 +01:00
"outputs": [],
2017-12-03 11:08:00 +01:00
"source": [
"class Punkt(object):\n",
" \"\"\"Klasa reprezentująca punkt w 2D.\"\"\"\n",
" def __init__(self, x, y):\n",
" \"\"\"opis argumentów x i y.\"\"\"\n",
" self.x = x\n",
" self.y = y"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 34,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"Help on class Punkt in module __main__:\n\nclass Punkt(__builtin__.object)\n | Klasa reprezentująca punkt w 2D.\n | \n | Methods defined here:\n | \n | __init__(self, x, y)\n | opis argumentów x i y.\n | \n | ----------------------------------------------------------------------\n | Data descriptors defined here:\n | \n | __dict__\n | dictionary for instance variables (if defined)\n | \n | __weakref__\n | list of weak references to the object (if defined)\n\n"
2017-12-03 11:08:00 +01:00
]
}
],
"source": [
"help(Punkt)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Enkapsulacja: publiczne czy prywatne?\n",
" * Prywatne zaczynają się od dwóch podkreśleń: ``__``, np. ``def __policz(self)``\n",
" * chronione tylko w konwencji, zaczynają się od '\\_', np. ``def _parse(self)``\n",
" * publiczne jest wszystko co nie zaczyna się od '\\_'."
]
},
{
"cell_type": "code",
2017-12-03 13:05:05 +01:00
"execution_count": 6,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Parser' object has no attribute '__parse'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
2017-12-03 13:05:05 +01:00
"\u001b[0;32m<ipython-input-6-80ee186598d3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mparser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mParser\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__parse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
2017-12-03 11:08:00 +01:00
"\u001b[0;31mAttributeError\u001b[0m: 'Parser' object has no attribute '__parse'"
2017-12-14 12:28:39 +01:00
],
"output_type": "error"
2017-12-03 11:08:00 +01:00
}
],
"source": [
"class Parser(object):\n",
" def __parse(self): pass\n",
" def _get(self): pass\n",
"parser = Parser()\n",
"parser._get()\n",
"parser.__parse()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Iteratory"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 42,
2017-12-03 11:08:00 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
2017-12-14 12:28:39 +01:00
"ename": "TypeError",
"evalue": "instance has no next() method",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-42-c424f51c19dc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mFigura\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mPunkt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: instance has no next() method"
],
"output_type": "error"
2017-12-03 11:08:00 +01:00
}
],
"source": [
"class Figura:\n",
" def __init__(self, vertexes):\n",
" self.vertexes = vertexes \n",
" \n",
" def __iter__(self):\n",
" self.index = -1\n",
" return self\n",
" \n",
" def __next__(self):\n",
" self.index += 1\n",
" if self.index == len(self.vertexes):\n",
" raise StopIteration\n",
" return self.vertexes[self.index]\n",
" \n",
"for v in Figura([Punkt(2,3), Punkt(3,4), Punkt(0, 0)]):\n",
" print(v)"
]
},
{
"cell_type": "markdown",
"metadata": {
2017-12-03 13:05:05 +01:00
"collapsed": true,
2017-12-03 11:08:00 +01:00
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2017-12-03 13:05:05 +01:00
"## Atrybuty i metody statyczne"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 1,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"79384200\n48959560\n0\n0\n"
2017-12-03 13:05:05 +01:00
]
}
],
"source": [
"class Klasa:\n",
" atrybut = 0\n",
"\n",
"klasa = Klasa()\n",
2017-12-14 12:28:39 +01:00
"#klasa2 = Klasa\n",
"#klasa2.atrybut=1\n",
"print(id(Klasa))\n",
"klasa2 = Klasa()\n",
"print(id(klasa2))\n",
"klasa2.atrybut=1\n",
"\n",
"print(Klasa.atrybut) \n",
"#atrybut wspólny dla wszystkich instancji\n",
"print(klasa.atrybut)\n",
"\n",
"klasa.temp = []"
2017-12-03 13:05:05 +01:00
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 62,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
2017-12-14 12:28:39 +01:00
"ename": "TypeError",
"evalue": "unbound method metoda() must be called with Klasa instance as first argument (got nothing instead)",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-62-63152cc2cac6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Jestem statyczna!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mKlasa\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmetoda\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: unbound method metoda() must be called with Klasa instance as first argument (got nothing instead)"
],
"output_type": "error"
2017-12-03 13:05:05 +01:00
}
],
"source": [
"class Klasa:\n",
" def __init__(self):\n",
" self.t = 0\n",
" def metoda():\n",
2017-12-14 12:28:39 +01:00
" # nie będzie działać self.t = 0\n",
2017-12-03 13:05:05 +01:00
" print(\"Jestem statyczna!\")\n",
"\n",
"Klasa.metoda()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Wyjątki"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 1,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
2017-12-14 12:28:39 +01:00
"ename": "IOError",
"evalue": "[Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'",
2017-12-03 13:05:05 +01:00
"traceback": [
2017-12-14 12:28:39 +01:00
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mIOError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-1bebecf2f5c5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"nieistniejący_plik.txt\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplik\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mcontent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplik\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'"
],
"output_type": "error"
2017-12-03 13:05:05 +01:00
}
],
"source": [
"with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 31,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
2017-12-14 12:28:39 +01:00
"from pandas.compat import FileNotFoundError\n",
"\n",
2017-12-03 13:05:05 +01:00
"try:\n",
" with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()\n",
"except FileNotFoundError:\n",
" contenct = \"\""
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 32,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"Warning [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'\n"
2017-12-03 13:05:05 +01:00
]
}
],
"source": [
"try:\n",
" with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()\n",
"except FileNotFoundError as e:\n",
" print(\"Warning {}\".format(e))\n",
" contenct = \"\""
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 13,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-12-14 12:28:39 +01:00
"Warning [Errno 2] No such file or directory: 'nieistniej\\xc4\\x85cy_plik.txt'\n"
2017-12-03 13:05:05 +01:00
]
}
],
"source": [
"try:\n",
" with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()\n",
"except Exception as e:\n",
" print(\"Warning {}\".format(e))\n",
" contenct = \"\""
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 14,
2017-12-03 13:05:05 +01:00
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"try:\n",
" with open(\"nieistniejący_plik.txt\") as plik:\n",
" content = plik.read()\n",
"except:\n",
" contenct = \"\""
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 19,
2017-12-03 13:05:05 +01:00
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
2017-12-14 12:28:39 +01:00
"outputs": [
{
"ename": "Exception",
"evalue": "Empty list of vertexes",
"traceback": [
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mException\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-19-0884e4d4e2f2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mfigura\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mFigura\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-19-0884e4d4e2f2>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, vertexes)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvertexes\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Empty list of vertexes\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvertexes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvertexes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mException\u001b[0m: Empty list of vertexes"
],
"output_type": "error"
}
],
2017-12-03 13:05:05 +01:00
"source": [
"class Figura:\n",
" def __init__(self, vertexes):\n",
" if len(vertexes) == 0:\n",
" raise Exception(\"Empty list of vertexes\")\n",
2017-12-14 12:28:39 +01:00
" self.vertexes = vertexes\n",
" \n",
"figura = Figura([])"
2017-12-03 13:05:05 +01:00
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 27,
2017-12-03 13:05:05 +01:00
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class MyError(Exception):\n",
" def __init__(self, text):\n",
" self.text = text\n",
" def __str__(self):\n",
" return self.text"
]
},
{
"cell_type": "code",
2017-12-14 12:28:39 +01:00
"execution_count": 34,
2017-12-03 13:05:05 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"ename": "MyError",
"evalue": "Coś poszło nie tak!",
"traceback": [
2017-12-14 12:28:39 +01:00
"\u001b[1;31m\u001b[0m",
"\u001b[1;31mMyError\u001b[0mTraceback (most recent call last)",
"\u001b[1;32m<ipython-input-34-99f654d8334c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mMyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Coś poszło nie tak!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mMyError\u001b[0m: Coś poszło nie tak!"
],
"output_type": "error"
2017-12-03 13:05:05 +01:00
}
],
"source": [
"raise MyError(\"Coś poszło nie tak!\")"
2017-12-03 11:08:00 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}