Skip to content

Commit 893089e

Browse files
committed
Add example for post-init hook
1 parent c8a3c45 commit 893089e

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

source-code/object-orientation/attr_intro.ipynb

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"cell_type": "code",
13-
"execution_count": 81,
13+
"execution_count": 2,
1414
"id": "ef9a778a-06a2-4dc0-941b-23c84b7c6418",
1515
"metadata": {},
1616
"outputs": [],
@@ -840,6 +840,87 @@
840840
"except ValueError as error:\n",
841841
" print(error)"
842842
]
843+
},
844+
{
845+
"cell_type": "markdown",
846+
"id": "771ff071-c15e-439a-b976-f7cb783a68ab",
847+
"metadata": {},
848+
"source": [
849+
"## Post-init hook"
850+
]
851+
},
852+
{
853+
"cell_type": "markdown",
854+
"id": "658da1f9-f5a0-46fa-b730-2cfb497e48d8",
855+
"metadata": {},
856+
"source": [
857+
"Sometimes you want to do initialization based on the values of the attributes passed to the constructor. You would typically do that in the `__init__` method. To do similar things for `attrs` classes, you can use the `__attrs_post_init__` method."
858+
]
859+
},
860+
{
861+
"cell_type": "code",
862+
"execution_count": 7,
863+
"id": "7a616115-ddd2-4582-ad3a-47b3ab7f8c64",
864+
"metadata": {},
865+
"outputs": [],
866+
"source": [
867+
"@attr.s\n",
868+
"class Guessing:\n",
869+
" nr_values : int = attr.ib(converter=int)\n",
870+
" _values : typing.List[int] = attr.ib(init=False)\n",
871+
"\n",
872+
" @nr_values.validator\n",
873+
" def nr_values_validate(self, attribute, value):\n",
874+
" if value < 1 or value > 10:\n",
875+
" raise ValueError('number of values should be between 1 and 10')\n",
876+
"\n",
877+
" def __attrs_post_init__(self):\n",
878+
" self._values = set()\n",
879+
" while len(self._values) < self.nr_values:\n",
880+
" self._values.add(random.choice(list(range(10))))\n",
881+
" \n",
882+
" def has_value(self, value):\n",
883+
" return value in self._values"
884+
]
885+
},
886+
{
887+
"cell_type": "code",
888+
"execution_count": 10,
889+
"id": "0cbea0f1-0f55-49f3-aaa2-54fd0006ca04",
890+
"metadata": {},
891+
"outputs": [
892+
{
893+
"name": "stdout",
894+
"output_type": "stream",
895+
"text": [
896+
"Guessing(nr_values=4, _values={0, 2, 5, 6})\n",
897+
"trying 7\n",
898+
"no luck with 7\n",
899+
"trying 4\n",
900+
"no luck with 4\n",
901+
"trying 8\n",
902+
"no luck with 8\n"
903+
]
904+
}
905+
],
906+
"source": [
907+
"guessing = Guessing(4)\n",
908+
"print(guessing)\n",
909+
"for guess in random.choices(range(10), k=3):\n",
910+
" print(f'trying {guess}')\n",
911+
" if guessing.has_value(guess):\n",
912+
" print(f'hurray for {guess}!')\n",
913+
" else:\n",
914+
" print(f'no luck with {guess}')"
915+
]
916+
},
917+
{
918+
"cell_type": "code",
919+
"execution_count": null,
920+
"id": "b84b2436-9a82-4872-a087-71fd4cbe5de4",
921+
"metadata": {},
922+
"outputs": [],
923+
"source": []
843924
}
844925
],
845926
"metadata": {
@@ -858,7 +939,7 @@
858939
"name": "python",
859940
"nbconvert_exporter": "python",
860941
"pygments_lexer": "ipython3",
861-
"version": "3.7.10"
942+
"version": "3.9.5"
862943
}
863944
},
864945
"nbformat": 4,

0 commit comments

Comments
 (0)