{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objektumorientált programozás II." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Exception
beépített osztályból kell öröklődni (opcionálisan mást is lehet bele rakni)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class KnightException(Exception):\n",
" pass\n",
"\n",
"lancelot = Person(\"Lancelot\", \"Mr\")\n",
"x = lancelot.__repr__()\n",
"if x[:3] != \"Sir\":\n",
" raise KnightException(\"a\", \"b\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterálható objektumok\n",
"Láttuk, hogy a `for i in L` nem csak akkor működik, ha `L` lista.\n",
"Pontosan mely objektumok állhatnak a for ... in
után?\n",
"\n",
"A `for` meghívja az `iter()` függvényt, mely egy iterálható objektumot ad vissza, melyre definiálva van egy `next()` függvény, amely minden meghívására visszaadja az objektum egy elemét. Ha a `next()` nem talál több elemet, egy `StopIteration` kivételt dob.\n",
"\n",
"Iterálható az az objektum, aminek van next
metódusa.\n",
"for ... in
után az állhat, aminek van __iter__ metódusa, ami iterálható objektumot ad vissza.\n",
"\n",
"Ezek **speciális** metódusok!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = range(3)\n",
"it = iter(r)\n",
"next(it)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print next(it)\n",
"print next(it)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"next(it)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tetszőleges, általunk definiált osztályhoz is adható iterátor tulajdonság. Először is szükség van egy `__iter__` függvényre, ami egy olyan objektummal tér vissza, ami iterálható. Az iterálható objekcumok (`list, set, tuple`) definiálnak egy `next()` metódust, ami sorban visszaadja az elemeket.\n",
"\n",
"Ha kifogyott, akkor **StopIteration** kivételt emel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Person(object):\n",
" def __init__(self, name, title=\"\"):\n",
" self.name = name\n",
" self.title = title\n",
" def __repr__(self):\n",
" return self.title + \" \" + self.name\n",
"\n",
"class Knight(Person):\n",
" title = 'Sir'\n",
" searching_for = 'The Holy Grail'\n",
" def __init__(self, name, eo):\n",
" super(Knight, self).__init__(name)\n",
" self.eo = eo\n",
" def __repr__(self):\n",
" return Knight.title + \" \" + self.name + \", \" + self.eo \n",
"\n",
"class Group(object):\n",
" def __init__(self, name, persons):\n",
" self.persons = persons\n",
" self.name = name\n",
" \n",
" def __iter__(self):\n",
" self.index = 0\n",
" return self\n",
" \n",
" def next(self):\n",
" if self.index >= len(self.persons):\n",
" raise StopIteration # dobunk egy kivetelt\n",
" self.index += 1\n",
" return self.persons[self.index - 1]\n",
" \n",
"kotrt = Group('Knights of The Round Table', \n",
" [Knight('Launcelot', 'the brave'), \n",
" Knight('Galahad', 'the pure'),\n",
" Knight('Bedevere', 'the wise'), \n",
" Knight('Robin', 'the Not-quite-so-brave-as-Sir-Launcelot')])\n",
"for knight in kotrt:\n",
" print knight"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vigyázzunk, hogy az indexet vissza kell állítani, különben csak egyszer tudunk iterálni! Persze mindezt megoldhattuk volna egyszerűbben."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Person(object):\n",
" def __init__(self, name, title=\"\"):\n",
" self.name = name\n",
" self.title = title\n",
" def __repr__(self):\n",
" return self.title + \" \" + self.name\n",
"\n",
"class Knight(Person):\n",
" title = 'Sir'\n",
" searching_for = 'The Holy Grail'\n",
" def __init__(self, name, eo):\n",
" super(Knight, self).__init__(name)\n",
" self.eo = eo\n",
" def __repr__(self):\n",
" return Knight.title + \" \" + self.name + \", \" + self.eo \n",
"\n",
"class Group:\n",
" def __init__(self, name, persons):\n",
" self.persons = persons\n",
" self.name = name\n",
" def __iter__(self):\n",
" return iter(self.persons)\n",
" \n",
"kotrt = Group('Knights of The Round Table', \n",
" [Knight('Launcelot', 'the brave'), \n",
" Knight('Galahad', 'the pure'),\n",
" Knight('Bedevere', 'the wise'), \n",
" Knight('Robin', 'the Not-quite-so-brave-as-Sir-Launcelot')])\n",
"for knight in kotrt:\n",
" print knight"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}