Created
January 22, 2017 12:29
-
-
Save koma5/f43f940dfdfa29f7188137c3734da369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"class HomoChildError(Exception):\n", | |
" \n", | |
" def __init__(self, value):\n", | |
" self.value = value\n", | |
" \n", | |
" def __str__(self):\n", | |
" return repr(self.value)\n", | |
"\n", | |
"class Human():\n", | |
" \n", | |
" def __init__(self, name, age, sex=None, mother=None, father=None):\n", | |
" self.name = name\n", | |
" self.age = age\n", | |
" self.sex = sex\n", | |
" self.mother = mother\n", | |
" self.father = father\n", | |
" \n", | |
" def __str__(self):\n", | |
" try:\n", | |
" return \"{} {} ({}, {})\".format(self.name, self.age, self.mother.name, self.father.name)\n", | |
" except AttributeError:\n", | |
" return \"{} {}\".format(self.name, self.age)\n", | |
" \n", | |
" def __gt__(self, other):\n", | |
" return self.age > other.age\n", | |
" \n", | |
" def __lt__(self, other):\n", | |
" return self.age < other.age\n", | |
" \n", | |
" def __ge__(self, other):\n", | |
" return self.age >= other.age\n", | |
" \n", | |
" def __le__(self, other):\n", | |
" return self.age <= other.age\n", | |
" \n", | |
" def __add__(self, other):\n", | |
" \n", | |
" if (self.sex == 'm' and other.sex == 'f'):\n", | |
" return Human('newName', 0, mother=other, father=self)\n", | |
" elif (other.sex == 'm' and self.sex == 'f'):\n", | |
" return Human('newName', 0, mother=self, father=other)\n", | |
" else:\n", | |
" raise HomoChildError(\"two men or two women can't have children!\")\n", | |
"\n", | |
" \n", | |
"class Woman(Human):\n", | |
" \n", | |
" def __init__(self, name, age, sex='f', mother=None, father=None):\n", | |
" Human.__init__(self, name, age, mother, father)\n", | |
"\n", | |
"\n", | |
"class Man(Human):\n", | |
" \n", | |
" def __init__(self, name, age, sex='m', mother=None, father=None):\n", | |
" Human.__init__(self, name, age, sex, mother, father)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"m = Human('Hugo', 20, 'm')\n", | |
"f = Human('Lara', 21, 'f')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Lea 0 (Lara, Hugo)\n", | |
"\n", | |
"Hugo 20\n" | |
] | |
} | |
], | |
"source": [ | |
"kind = m + f\n", | |
"kind.name = \"Lea\"\n", | |
"\n", | |
"print(str(kind) + '\\n')\n", | |
"\n", | |
"print(kind.father)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"print(kind < f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Leonie 30\n" | |
] | |
} | |
], | |
"source": [ | |
"print(Woman('Leonie', 30))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "HomoChildError", | |
"evalue": "\"two men or two women can't have children!\"", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mHomoChildError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-10-6a3ccd21e6e4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mm1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHuman\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Reto'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m42\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mm2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHuman\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Remo'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m41\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mm2\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mm1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-5-8081e56ff084>\u001b[0m in \u001b[0;36m__add__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mHuman\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'newName'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmother\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfather\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 43\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mHomoChildError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"two men or two women can't have children!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 44\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mHomoChildError\u001b[0m: \"two men or two women can't have children!\"" | |
] | |
} | |
], | |
"source": [ | |
"m1 = Human('Reto', 42, 'm')\n", | |
"m2 = Human('Remo', 41, 'm')\n", | |
"m2 + m1" | |
] | |
} | |
], | |
"metadata": { | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment