Last active
July 3, 2020 17:34
-
-
Save ljbelenky/fee785cb46aa241ccfa34ba1b20b01fa 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Examples of using setters and getters\n", | |
"\n", | |
"## Note:\n", | |
"- each individual method is very, very short: calculates only the value needed at that moment\n", | |
"- All values (except YOB) are calculated *when called*, so they are always based on most recent data\n", | |
"- Changing YOB changes all values derived from it. Values never get out-of-sync or in conflict\n", | |
"- Some values cannot be changed\n", | |
"- Other values can be changed, but changing them triggers other actions\n", | |
"- Details of calculations are hidden from the end user. You don't need to know how old one must be to run for president. The class knows that and keeps that detail internally" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Person:\n", | |
" def __init__(self, yob):\n", | |
" self.yob = yob\n", | |
" \n", | |
" @property\n", | |
" def age(self):\n", | |
" return 2020 - self.yob\n", | |
" \n", | |
" @age.setter\n", | |
" def age(self, new_age):\n", | |
" print('changing age, therefore adjusting birthday')\n", | |
" self.yob = 2020 - new_age\n", | |
" print(f'Year of birth has been adjusted to {self.yob}')\n", | |
" \n", | |
" @property\n", | |
" def old_enough_to_vote(self):\n", | |
" return self.age > 18\n", | |
" \n", | |
" @property\n", | |
" def old_enough_to_drink(self):\n", | |
" return self.age > 21\n", | |
" \n", | |
" @property\n", | |
" def old_enough_to_run_for_president(self):\n", | |
" return self.age > 35\n", | |
" \n", | |
" @old_enough_to_run_for_president.setter\n", | |
" def old_enough_to_run_for_president(self, value):\n", | |
" print(\"Nah, you can't change that value. Consider changing your age or YOB instead\")\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land = Person(1976)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.age" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.old_enough_to_drink" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.old_enough_to_run_for_president" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.old_enough_to_run_for_president = False" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.old_enough_to_run_for_president" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.age = 22" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Land.old_enough_to_run_for_president" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!