05-08 update

This commit is contained in:
James Turk 2024-10-27 19:11:35 -05:00
parent a2108132c7
commit f1d43d8e19
4 changed files with 975 additions and 732 deletions

View File

@ -44,21 +44,12 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "72bee260-b4d1-4c61-a689-8799d4dfb7c6",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"echo = <function echo at 0x10760c820>\n",
"type(echo) = <class 'function'>\n"
]
}
],
"outputs": [],
"source": [
"def echo(message):\n",
" print(message)\n",
@ -70,21 +61,12 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "39ee1708-c3ec-47ed-9c87-2124fa55b57f",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n",
"hello\n"
]
}
],
"outputs": [],
"source": [
"# we can assign other names to objects, including functions\n",
"\n",
@ -95,48 +77,24 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "5a175699-fd00-4e69-a534-2d25085550d2",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(4418750496, 4418750496)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"id(x), id(echo)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "52de62a9-af2f-4fbe-becf-2c315ddb9c77",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"1\n",
"2\n",
"3\n",
"3\n"
]
}
],
"outputs": [],
"source": [
"# we can also store functions in other types\n",
"\n",
@ -147,27 +105,17 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "6cad8105-8a71-4b8a-926c-dfa0f635e7ad",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"twice\n",
"twice\n",
"once\n"
]
}
],
"outputs": [],
"source": [
"# dictionaries too\n",
"func_mapping = {False: print, True: echo}\n",
"\n",
"print_twice = True\n",
"print_twice = f()\n",
"func_mapping[True](\"twice\")\n",
"\n",
"print_twice = False\n",
@ -184,21 +132,12 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"id": "d89416fb-e0e7-48ca-9ca7-9fea5525b273",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"add, 3, 4 = 7\n",
"sub, 3, 4 = -1\n"
]
}
],
"outputs": [],
"source": [
"# we can pass functions into other functions\n",
"\n",
@ -246,22 +185,23 @@
"outputs": [],
"source": [
"fn = get_op(\"mod\")\n",
"perform_op(fn, 10, 3)"
"fn(100, 5)\n",
"#perform_op(fn, 10, 3)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"id": "c898e181-1fcd-477e-b6bf-573544ed6170",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"x = [(\"Nick\", 1), (\"Yusong\", 9000), (\"Emma\", 100)]\n",
"x = [(\"Nick\", 1), (\"Nick\", -100), (\"Yusong\", 9000), (\"Emma\", 100)]\n",
"\n",
"def negate(y):\n",
" return -y\n",
" return -y[1]\n",
"\n",
"def second_key(item):\n",
" return item[1]\n"
@ -269,58 +209,41 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"id": "ff9cfd44-8a22-4c83-98a6-4420ce651238",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Nick', 1), ('Emma', 100), ('Yusong', 9000)]\n"
]
}
],
"outputs": [],
"source": [
"x.sort(key=second_key)\n",
"x.sort(key=negate)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "9422326e-37ad-4ffe-8e03-4a0bd76df9de",
"metadata": {},
"outputs": [],
"source": [
"help(sorted)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73f8aee0-8a13-43cf-b565-96285891dd4f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"second_key(x[0])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "8c3c8074-4696-483a-9a16-238caec889f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Emma', 100), ('Nick', 1), ('Yusong', 9)]\n"
]
}
],
"outputs": [],
"source": [
"x.sort()\n",
"print(x)"
@ -440,23 +363,12 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"id": "68f51efb-9a62-4303-afe7-3639be9cc395",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# can fit places a function definition can't\n",
"# such as being used as a parameter\n",
@ -475,28 +387,17 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"id": "9b6748b0-9fb0-4664-86c6-bc35e4c5d9e2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['abc', 'Abc', 'ABC', 'AbC']"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sorted([\"abc\", \"Abc\", \"ABC\", \"AbC\"], key=lambda s: s.upper())"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "39bf652e-f85d-4a66-96bf-0ae21054be26",
"metadata": {},
"outputs": [],
@ -512,23 +413,12 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"id": "1eb1c524-30e1-4b20-b2a9-7b078e906c61",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(function, function)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"type(mul), type(mul2)"
]
@ -595,7 +485,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "3e2768f5-8817-4dfc-8ff5-72a774f499fa",
"metadata": {
"tags": []
@ -605,14 +495,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
"called add_two 1\n",
"3\n",
"called add_two 2\n",
"4\n",
"called add_two 3\n",
"5\n"
]
}
],
"source": [
"def add_two(x):\n",
" print(\"called add_two\", x)\n",
" return x + 2\n",
"\n",
"for x in map(add_two, [1, 2, 3]):\n",
@ -621,7 +515,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "cd940ce1-c28a-4646-8846-f199f586cad8",
"metadata": {
"tags": []
@ -668,7 +562,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"id": "04106e30-95df-432a-b6b0-40fcc58f51e5",
"metadata": {
"tags": []
@ -678,6 +572,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"called add_two 1\n",
"called add_two 2\n",
"called add_two 3\n",
"[3, 4, 5]\n"
]
}
@ -689,7 +586,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 8,
"id": "fba295ed-9dcb-4cbb-8b7e-3b0ff2965639",
"metadata": {
"tags": []
@ -713,7 +610,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"id": "18f5223a-b85a-44c6-9f49-40183a1e3208",
"metadata": {},
"outputs": [
@ -729,13 +626,13 @@
],
"source": [
"# number of parameters must match number of iterables\n",
"for x in map(lambda x, y, z: x+y*z, (\"A\", \"B\", \"C\"), [\"!\", \"?\", \".\"], [2, 3, 4]):\n",
"for x in map(lambda x, y, z: x+(y*z), (\"A\", \"B\", \"C\"), [\"!\", \"?\", \".\"], [2, 3, 4]):\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"id": "c4e1d7f9-c111-44ff-8525-de147f90aa56",
"metadata": {
"tags": []
@ -747,7 +644,7 @@
"15"
]
},
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -760,7 +657,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 14,
"id": "e6383ce1-6e06-44a4-beb2-1e946b81c483",
"metadata": {
"tags": []
@ -769,22 +666,21 @@
{
"data": {
"text/plain": [
"[10, 10]"
"{10}"
]
},
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"list(map(operator.sub, [20, 19], [10, 9]))"
"set(map(operator.sub, [20, 19], [10, 9]))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 17,
"id": "b58441e4-ba95-43e4-8151-dd25e1c7efd1",
"metadata": {
"tags": []
@ -793,10 +689,10 @@
{
"data": {
"text/plain": [
"{'AAA', 'BBB', 'CCC'}"
"('AAA', 'BBB', 'CCC')"
]
},
"execution_count": 20,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@ -806,7 +702,7 @@
"\n",
"# possible to pass into set or list \n",
"# or use anywhere you can use an iterable\n",
"set(map(lambda x: x * 3, (\"A\", \"B\", \"C\")))"
"tuple(map(lambda x: x * 3, (\"A\", \"B\", \"C\")))"
]
},
{
@ -822,15 +718,13 @@
{
"cell_type": "code",
"execution_count": 21,
"id": "7eaab05d-2b8e-45fa-ad97-565c2edac0f6",
"metadata": {
"tags": []
},
"id": "62cac0ce-56e4-4ca7-9223-ed9be407428a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Abc', 'Xyz']"
"['ABC', 'XYZ']"
]
},
"execution_count": 21,
@ -839,13 +733,13 @@
}
],
"source": [
"list(map(lambda s: s.title(), filter(str.isupper, [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
"list(filter(lambda s: s.isupper(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\", \"\"]))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "c4a69097-c30c-4096-b69b-f3fa89a1bc7f",
"id": "7eaab05d-2b8e-45fa-ad97-565c2edac0f6",
"metadata": {
"tags": []
},
@ -853,7 +747,7 @@
{
"data": {
"text/plain": [
"['ABC', 'XYZ']"
"['ABCABC', 'XYZXYZ']"
]
},
"execution_count": 22,
@ -862,13 +756,13 @@
}
],
"source": [
"list(filter(lambda s: s.isupper(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"]))"
"list(map(lambda s: s*2, filter(str.isupper, [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "6852f961-f20d-4c82-8da4-c7bdcfc1ae4b",
"id": "c4a69097-c30c-4096-b69b-f3fa89a1bc7f",
"metadata": {
"tags": []
},
@ -876,7 +770,7 @@
{
"data": {
"text/plain": [
"['abc', 'xyz']"
"['A']"
]
},
"execution_count": 23,
@ -884,27 +778,28 @@
"output_type": "execute_result"
}
],
"source": [
"list(filter(str.isupper, map(lambda s: s.title(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6852f961-f20d-4c82-8da4-c7bdcfc1ae4b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"list(map(lambda s: s.lower(), filter(lambda s: s.isupper(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "a195957c-0e89-4d3e-9b85-ace9e1f84e5d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"g = (x**2 for x in filter(lambda x: x % 2 != 0, range(20)))\n",
"list(g)"
@ -941,17 +836,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"id": "a623edc8-08cb-41d0-8b2c-fd99619fed73",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import functools \n",
"import operator \n",
"\n",
"#accumulator = 0\n",
"#for item in my_list:\n",
"# accumulator += item\n",
"# accumulator = 0\n",
"# for item in my_list:\n",
"# accumulator += item\n",
"\n",
"# 1st iteration: Call operator.add(1,2) -> 3 \n",
"# 2nd iteration: Call operator.add(3,3) -> 6 \n",
@ -962,24 +868,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"id": "5081e0e6-424b-472b-b0b0-f0c2bfa1344e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"names = [\"Ben\", \"Martha\", \"Susan\"]\n",
"# 1st iteration: call f(0, \"Ben\") -> 0 + len(\"Ben\") -> 3\n",
"# 2nd iteration: call f(3, \"Martha\") -> 3 + len(\"Martha\") -> 9\n",
"# 3rd iteration: call f(9, \"Susan\") -> 9 + len(\"Susan\") -> 14\n",
"functools.reduce(lambda a, b: a + len(b), names, 0)"
"functools.reduce(lambda accumulator, new_val: accumulator + len(new_val), \n",
" names, \n",
" 0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"id": "6fd2afda-b07f-45f6-9a82-6bc50665af6c",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"48"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# What happens if you pass in an initial value \n",
"# 1st iteration: Call operator.mul(2,1) -> 2 \n",
@ -987,7 +917,7 @@
"# 3rd iteration: Call operator.mul(4,3) -> 12 \n",
"# 4th iteration: Call operator.mul(12,4) -> 48 \n",
"# Final result = 48 \n",
"functools.reduce(operator.mul, [1,2,3,4], 0)"
"functools.reduce(operator.mul, [1,2,3,4], 2)"
]
},
{
@ -999,7 +929,7 @@
},
"outputs": [],
"source": [
"functools.reduce(lambda a,b: a+b, [], 0)"
"functools.reduce(lambda a,b: a+b, [1, 2, 3])"
]
},
{
@ -1014,7 +944,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 31,
"id": "5a23eb2c-8324-4ff1-beb2-bca7ff5dbbe1",
"metadata": {},
"outputs": [
@ -1024,7 +954,7 @@
"20"
]
},
"execution_count": 25,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
@ -1036,7 +966,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 32,
"id": "70988dc5-1a32-4316-bb9a-d4be646dd2c2",
"metadata": {},
"outputs": [
@ -1046,7 +976,7 @@
"-5"
]
},
"execution_count": 26,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@ -1059,7 +989,28 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 35,
"id": "995d61df-dfcf-411a-b371-7633ec9ed649",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-1, -2, -3, -4]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(negate, [1, 2, 3, 4]))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "7b2d87c2-903a-4b48-a012-3e3b0b03584f",
"metadata": {},
"outputs": [
@ -1086,7 +1037,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 37,
"id": "0e2ffbc6-e05f-401e-b569-273c61f6d514",
"metadata": {},
"outputs": [
@ -1094,20 +1045,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"x!a!b!c\n",
"a!b!c\n",
"x!a!b!c\n"
]
}
],
"source": [
"print_ex = functools.partial(print, \"x\", sep=\"!\")\n",
"print_ex(\"a\", \"b\", \"c\")\n",
"print(\"x\", \"a\", \"b\", \"c\", sep=\"!\")"
"print_ex = functools.partial(print, sep=\"!\")\n",
"print_ex(\"a\", \"b\", \"c\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"id": "6e9be6b5-0a68-4de6-8289-31fb5095d31f",
"metadata": {
"tags": []
@ -1120,31 +1070,19 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"id": "10c4769b-7b1f-4fbc-924d-8b3e5de7fb93",
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'foo' is an invalid keyword argument for print()",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[30], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mprint_foo\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: 'foo' is an invalid keyword argument for print()"
]
}
],
"outputs": [],
"source": [
"print_foo(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"id": "318556fb-eafa-44af-bcfd-0848b87b6e18",
"metadata": {},
"outputs": [],
@ -1158,7 +1096,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"id": "9b4c5678-2525-4822-93e3-77dd0f40213b",
"metadata": {
"tags": []
@ -1198,21 +1136,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 40,
"id": "79029822-694a-445d-9776-48e05e94733b",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]\n"
]
}
],
"source": [
"powers_of_two = [2 ** n for n in range(10)]\n",
"#f = lambda n: n ** 2\n",
"powers_of_two = [2**n for n in range(10)]\n",
"print(powers_of_two)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"id": "fe7d9640-165c-4887-a9f0-9e411fc8e031",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Q♠', 'Q♣', 'Q♦', 'Q♥', 'J♠', 'J♣', 'J♦', 'J♥']\n"
]
}
],
"source": [
"# possible to nest comprehensions, but beware readability\n",
"faces = (\"K\", \"Q\", \"J\")\n",
@ -1233,10 +1188,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 42,
"id": "24213ab6-3c6e-45df-8bb9-93b2d69c10fa",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{8, 16, 2, 4}\n"
]
}
],
"source": [
"powers_of_two_set = {2 ** n for n in [1,1,2,2,3,3,3,4,4,4]}\n",
"print(powers_of_two_set)"
@ -1244,21 +1207,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"id": "95f3d657-39aa-4803-8fc7-4e736ebc5b20",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{3: 2, 4: 3, 5: 4, 6: 5}\n"
]
}
],
"source": [
"\n",
"powers_of_two_mapping = {n: f(n) for n in range(5) if n > 0}\n",
"powers_of_two_mapping = {n+2:n+1 for n in range(5) if n > 0}\n",
"print(powers_of_two_mapping)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 47,
"id": "7139c632-97cf-49a5-8abf-5b7516c1a401",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object <genexpr> at 0x1037de5e0>\n"
]
}
],
"source": [
"p2gen = (2 ** n for n in [1,1,2,2,3,3,3,4,4,4])\n",
"print(p2gen)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16f356d0-5147-455d-b7b4-385bd544c56a",
"metadata": {},
"outputs": [],
"source": []
}

View File

@ -32,29 +32,29 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "2260b250-1ae6-4d3b-ab05-314ddad19256",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object simple_generator_func at 0x1064afd80>"
"generator"
]
},
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# return type of a yielding function is a generator\n",
"simple_generator_func()"
"type(simple_generator_func())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"id": "c051b45d-9a37-4086-b90c-2090110c8cc6",
"metadata": {
"tags": []
@ -64,6 +64,35 @@
"g = simple_generator_func()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0d55a761-1496-46dc-bafe-4eb121ad0ca7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start\n",
"value from generator = 1\n",
"between loops\n",
"still running\n",
"2\n",
"one more\n",
"3\n"
]
}
],
"source": [
"for x in g:\n",
" print(\"value from generator = \", x)\n",
" break\n",
"print(\"between loops\")\n",
"for x in g:\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"id": "427fe881-459e-4a20-acfa-dae65a1b906c",
@ -76,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 15,
"id": "6a87d7d2-9eb5-4415-9770-79d9594aafa5",
"metadata": {},
"outputs": [
@ -87,20 +116,25 @@
"start\n",
"yielded value= 1\n",
"still running\n",
"yielded value= 2\n"
"yielded value= 2\n",
"one more\n",
"next= 3\n"
]
}
],
"source": [
"for x in simple_generator_func():\n",
"g = simple_generator_func()\n",
"for x in g:\n",
" print(\"yielded value=\", x)\n",
" if x == 2:\n",
" break"
" break\n",
"\n",
"print(\"next=\", next(g))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 16,
"id": "544d5a37-a1c0-47f6-b270-429ea2215386",
"metadata": {},
"outputs": [],
@ -113,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 19,
"id": "7fbc6cf5-0995-4d20-b47d-6b5454899168",
"metadata": {},
"outputs": [
@ -126,20 +160,58 @@
"6\n",
"8\n",
"10\n",
"12\n"
"12\n",
"14\n",
"16\n",
"18\n",
"20\n",
"22\n",
"24\n",
"26\n",
"28\n",
"30\n",
"32\n",
"34\n",
"36\n",
"38\n",
"40\n",
"42\n"
]
}
],
"source": [
"for evens in evens_up_to(10000000000000):\n",
"#n = 0\n",
"for evens in evens_up_to(2000000000000000000):\n",
" # n += 1\n",
" print(evens)\n",
" if evens > 10:\n",
" if evens > 40:\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 24,
"id": "5af8b087-6b63-4948-811d-5ba4e0a8ba55",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 100000000000000000000)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(100000000000000000000))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "2fbf706b-6f6c-4a24-890e-10121d709582",
"metadata": {},
"outputs": [],
@ -154,17 +226,17 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "32920b91-7b00-4b2a-8927-07bd5cbc6fa2",
"metadata": {},
"outputs": [],
"source": [
"# list(powers_of_two())"
"list(powers_of_two())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 26,
"id": "27a8bd75-3912-4f97-9655-763279f07f9b",
"metadata": {},
"outputs": [
@ -190,41 +262,85 @@
},
{
"cell_type": "code",
"execution_count": 10,
"id": "dd4f4d70-e74a-43d3-88bc-7dfe04add2a2",
"metadata": {
"tags": []
},
"execution_count": 27,
"id": "5c3b2178-65fc-43d2-8940-d91bf58e1881",
"metadata": {},
"outputs": [],
"source": [
"g = powers_of_two()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "396ce187-a2e3-4611-a188-9403f17cba34",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 1000000000000000000)"
"16384"
]
},
"execution_count": 10,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(1000000000000000000)"
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "dd4f4d70-e74a-43d3-88bc-7dfe04add2a2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"for x in range(100000000000000000000000000000000):\n",
" if x > 5:\n",
" break\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5ba1490-5fe1-4120-95a0-f60f85559959",
"metadata": {},
"outputs": [],
"source": [
"y = list(range(100000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb4db8a4-36da-4d56-b8bd-c10b6e01fc73",
"metadata": {},
"outputs": [],
"source": [
"len(y)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "9be584c1-4cf9-4ad3-88ab-4b5da18e42da",
"metadata": {},
"outputs": [],
"source": [
"g = powers_of_two()\n",
"h = powers_of_two()"
"h = powers_of_two()\n",
"k = h"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 43,
"id": "81095f30-547f-4624-b2a4-9cc5997c4a8b",
"metadata": {
"tags": []
@ -236,7 +352,7 @@
"True"
]
},
"execution_count": 12,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
@ -247,40 +363,17 @@
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e34470cd-efdd-45d0-a1b5-58e478260e4c",
"execution_count": 77,
"id": "c9a9fa0a-e699-4a56-9e9a-cd7e07f1a446",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
"128"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "2e656696-8e3e-4dfd-86f9-9b87a08debe3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 14,
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
@ -291,23 +384,231 @@
},
{
"cell_type": "code",
"execution_count": 15,
"id": "39b39657-243c-4641-9859-90fdb7adb3fc",
"execution_count": 78,
"id": "9b019479-cb3a-41b4-8ef8-a62e61b56430",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"range(0, 100)\n"
]
"data": {
"text/plain": [
"256"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(k)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "07cb4313-a300-47c3-b873-b074399a2fad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4427895344"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(g)\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "3e2ea606-1041-4171-9096-837fe1eaacdd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4427893440"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(h)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "e34470cd-efdd-45d0-a1b5-58e478260e4c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4096"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "2e656696-8e3e-4dfd-86f9-9b87a08debe3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"128"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(h)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39b39657-243c-4641-9859-90fdb7adb3fc",
"metadata": {},
"outputs": [],
"source": [
"r = range(100)\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f746d52-4f7f-4a97-aaeb-330e99aaf722",
"metadata": {},
"outputs": [],
"source": [
"# what happens if the parameter changes during iteration?\n",
"def gen_test(param):\n",
" # param = ...bound...\n",
" i = 0\n",
" while i < param:\n",
" yield i\n",
" i += 1\n",
"\n",
"bound = 5\n",
"for x in gen_test(bound):\n",
" print(\"x is \", x, \"bound is\", bound)\n",
" bound.append(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66d6ae08-da37-4f90-8a77-db08205c3b17",
"metadata": {},
"outputs": [],
"source": [
"def gen_test2():\n",
" yield 1\n",
" yield 2\n",
" yield 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88b2aaee-b02e-44f5-8a12-2ba2a539978c",
"metadata": {},
"outputs": [],
"source": [
"g = gen_test2()\n",
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e47ea06-b792-415c-9af9-a850b82b3c95",
"metadata": {},
"outputs": [],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a7803f2-4751-4f41-b1e4-04a212da71aa",
"metadata": {},
"outputs": [],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8081044-cab9-4cd3-9f03-d50fc5ad1362",
"metadata": {},
"outputs": [],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40156c07-49d1-4753-9f26-0fe2481e9a42",
"metadata": {},
"outputs": [],
"source": [
"x = [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee0103a6-daf2-457b-af6a-913d7fdb9f92",
"metadata": {},
"outputs": [],
"source": [
"g = iter(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6d86fd8-a441-48c2-847e-e56b576102ae",
"metadata": {},
"outputs": [],
"source": [
"def newrange(lower_bound, upper_bound):\n",
" i = lower_bound\n",
" while i < upper_bound:\n",
" yield i\n",
" i += 1"
]
},
{
"cell_type": "markdown",
"id": "308fc5a9-78ac-40cd-afac-970614e9460e",
@ -354,16 +655,17 @@
"Equivalent to:\n",
"\n",
"```python\n",
"def g():\n",
"def gf():\n",
" for var in iterable:\n",
" if condition:\n",
" yield expression\n",
"g = gf()\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 79,
"id": "aa9b4c18-3d73-4d9c-8e78-3dba56b4a717",
"metadata": {},
"outputs": [
@ -371,7 +673,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object <genexpr> at 0x107f1cf90>\n"
"<generator object <genexpr> at 0x107ec63b0>\n"
]
}
],
@ -392,7 +694,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 80,
"id": "729f6476-491a-4daf-b460-23f602ef18fa",
"metadata": {},
"outputs": [
@ -434,7 +736,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 81,
"id": "cfed52c0-0467-4b50-ac6c-66d6fa694ec6",
"metadata": {
"tags": []
@ -460,7 +762,28 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 89,
"id": "386c0eea-ea29-48bf-ba1b-dc174f340f18",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4294967297"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(pow_generator)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "ac1be105-edc6-4b52-8715-e54c186861d2",
"metadata": {},
"outputs": [
@ -470,7 +793,7 @@
"[0, 4, 16, 36, 64]"
]
},
"execution_count": 49,
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
@ -483,36 +806,227 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 92,
"id": "c721ff55-86ac-46b1-826f-4613905162c8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# equivalent with map & filter\n",
"ll = range(10)\n",
"g = map(lambda x: x**2, filter(lambda x: x % 2 == 0, ll))"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "a5aba588-3823-4763-b1ab-5a6217f72f8e",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 4, 16, 36, 64]"
"0"
]
},
"execution_count": 52,
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# equivalent with map & filter\n",
"ll = range(10)\n",
"list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, ll)))"
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "2452f706-096d-4bca-aae7-cc6564e48a23",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "8a250ce6-7e8e-4915-9ea0-1751a96046f0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "8949fbea-455c-483b-83b1-66b9c70fdf30",
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[98], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "832bd2a5-cf99-40bf-9dd3-7a2833589485",
"metadata": {},
"outputs": [],
"source": [
"ll = [1, 2, 3, 4]\n",
"g = iter(ll)\n"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "01662fa6-dfb9-4609-a04f-9394c7b63a16",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "6c353acf-93a7-47c8-8016-90b9b407767d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "e0e9d459-7d96-4187-ba3e-8f8e6a69eb5d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "11f9029d-5029-496c-894e-b416d5b308d5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "8a94d0a3-d09b-4ab7-a465-93c7a6e1511e",
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[104], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5aba588-3823-4763-b1ab-5a6217f72f8e",
"metadata": {
"tags": []
},
"id": "c7250129-eab1-4fec-bb46-917e7c11f01e",
"metadata": {},
"outputs": [],
"source": []
}

View File

@ -39,7 +39,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "bf892e39-64d4-417c-92dc-353b396c60c9",
"metadata": {},
"outputs": [],
@ -50,6 +50,8 @@
" coordinate = (100, 100)\n",
" ...\n",
"\n",
"paint(RED)\n",
"\n",
"# How many globals? \n",
"# How many locals inside paint?\n",
"\n",
@ -59,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "d2a780bf-44bd-47f5-9de6-5ba905e8f4f3",
"metadata": {},
"outputs": [
@ -155,24 +157,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "7beecb5d-3cb8-42e3-ad94-689eebf603bc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"2\n"
]
}
],
"outputs": [],
"source": [
"## Without global declaration \n",
"x = 2\n",
"def f():\n",
" x = 2\n",
" # x = 2\n",
" x += 1\n",
" print(x) # prints: 3\n",
"f() \n",
@ -181,26 +174,33 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"id": "74a02260-838d-43d6-ab6e-c38d13410346",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"3\n"
"ename": "UnboundLocalError",
"evalue": "local variable 'x' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[7], line 10\u001b[0m\n\u001b[1;32m 8\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(y) \n\u001b[0;32m---> 10\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \n\u001b[1;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(x) \u001b[38;5;66;03m# global scope x was modified\u001b[39;00m\n",
"Cell \u001b[0;32mIn[7], line 7\u001b[0m, in \u001b[0;36mf\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf\u001b[39m():\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#global x\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m#x = 1\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m#x += 1\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[43mx\u001b[49m\n\u001b[1;32m 8\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(y)\n",
"\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'x' referenced before assignment"
]
}
],
"source": [
"## With global declaration \n",
"x = 2\n",
"x = 5\n",
"def f():\n",
" global x\n",
" x += 1\n",
" print(x) # prints: 3\n",
" #global x\n",
" #x = 1\n",
" #x += 1\n",
" y = x\n",
" x = 5\n",
" print(y) \n",
"f() \n",
"print(x) # global scope x was modified"
]
@ -217,25 +217,25 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"id": "8cdea7b4-8b72-431f-b6c1-a7756eb3c451",
"metadata": {},
"outputs": [],
"source": [
"def f1():\n",
" x = \"outer\" \n",
" x = \"OUTER\" \n",
" def f2():\n",
" # nonlocal x\n",
" # x = \"inner\"\n",
" print(\"inside f2\", x)\n",
" print(\"inside f1 before f2 has been called\", x)\n",
" return f2\n",
" #print(\"inside f1 after f2 has been called\", x)"
" nonlocal x\n",
" x = \"INNER\"\n",
" print(\"inside f2 x=\", x)\n",
" print(\"inside f1 before f2 has been called x=\", x)\n",
" f2()\n",
" print(\"inside f1 after f2 has been called\", x)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 11,
"id": "03894ba5-f23d-4de3-80ff-08422952ffd5",
"metadata": {},
"outputs": [
@ -243,7 +243,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"inside f1 before f2 has been called outer\n"
"inside f1 before f2 has been called x= OUTER\n",
"inside f2 x= INNER\n",
"inside f1 after f2 has been called INNER\n"
]
}
],
@ -253,27 +255,19 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "60faa092-ee97-4c43-aa0a-64e607e5d432",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inside f2 outer\n"
]
}
],
"outputs": [],
"source": [
"inner_func()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 12,
"id": "21531a6f-cb70-4ca9-b030-7928487b71d7",
"metadata": {
"tags": []
@ -288,13 +282,13 @@
" print(f\"called {counter} times\")\n",
" return f\n",
"\n",
"f = create_counter_func()\n",
"g = create_counter_func()"
"g = create_counter_func()\n",
"h = create_counter_func()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 18,
"id": "1e486a39-efa4-47eb-bdf2-7bd2038c37f8",
"metadata": {
"tags": []
@ -304,17 +298,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"called 1 times\n"
"called 5 times\n"
]
}
],
"source": [
"f()"
"h()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 22,
"id": "9f12ec73-ee3e-4ed6-8c7c-603cd8238dec",
"metadata": {
"tags": []
@ -324,7 +318,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"called 1 times\n"
"called 5 times\n"
]
}
],
@ -346,24 +340,23 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 24,
"id": "43fa84fb-f7c4-4b46-a882-d2d31b1a23f5",
"metadata": {},
"outputs": [],
"source": [
"def make_func(n):\n",
" #y = 3\n",
" def f(x):\n",
" # n: locally scoped to make_func() < enclosing scope\n",
" # x: locally scoped to f()\n",
" # we are using the n from the enclosing scope\n",
" return x ** n # y\n",
" return x ** n \n",
" return f"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 25,
"id": "acaf5a72-aac2-4fd4-aa39-4423998fca68",
"metadata": {},
"outputs": [
@ -373,7 +366,7 @@
"1000"
]
},
"execution_count": 12,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@ -385,29 +378,28 @@
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1d4fc6ca-cd11-446c-9530-c85bc09cc34c",
"execution_count": null,
"id": "4c16d356-8c0c-4fc7-a6ff-bbab09a56006",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"squared = make_func(2)\n",
"squared(55)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d4fc6ca-cd11-446c-9530-c85bc09cc34c",
"metadata": {},
"outputs": [],
"source": [
"squared(10)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "3546e348-e3a6-4516-a14a-fc2cf63e8167",
"metadata": {},
"outputs": [],
@ -424,7 +416,8 @@
" answer = math.sin(x) + math.exp(y)\n",
" # save to cache\n",
" prior_calls[x, y] = answer\n",
" \n",
"\n",
" print(\"cache=\", prior_calls)\n",
" # retrieve from cache\n",
" return prior_calls[x, y]\n",
" \n",
@ -443,77 +436,40 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"id": "eebb454b-7414-46a6-8a08-f02cca13ed76",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing computation on 1 and 2...\n"
]
},
{
"data": {
"text/plain": [
"8.230527083738547"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"do_computation(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"id": "50ac829c-0ca5-433b-a2ec-6c0bde23ee3f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8.230527083738547"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"do_computation(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"id": "4f9ce6dd-2d77-431d-9f76-f5a175dd47f4",
"metadata": {},
"outputs": [],
"source": [
"do_computation(0.5, 0.7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "848c513c-5d15-451a-afb2-5ff24fb7c7f2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing computation on 1 and 2...\n"
]
},
{
"data": {
"text/plain": [
"8.230527083738547"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"do_computation2 = make_cached_calc()\n",
"do_computation2(1, 2)"

View File

@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"id": "d8cecafd-c460-446b-8f8f-7a35a9f8c6e2",
"metadata": {},
"outputs": [],
@ -37,18 +37,18 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 8,
"id": "f327b88e-d034-4bd3-9e63-9196ccbb3d84",
"metadata": {},
"outputs": [],
"source": [
"def inner():\n",
" print(\"inner function\")"
"def inner(a, b, c):\n",
" print(\"inner function\", a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"id": "359d03fc-1d69-4574-a06c-45b55905cecb",
"metadata": {},
"outputs": [
@ -56,22 +56,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
"BEFORE <function inner at 0x1038697e0>\n",
"inner function\n",
"AFTER <function inner at 0x1038697e0>\n"
"BEFORE <function inner at 0x103591240>\n",
"inner function 1 2 3\n",
"AFTER <function inner at 0x103591240>\n"
]
}
],
"source": [
"wrapped_inner = print_before_and_after(inner)\n",
"\n",
"wrapped_inner()\n",
"wrapped_inner(1, 2, 3)\n",
"#print(x)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"id": "916c438d-f0f3-4b78-9846-f9bf2aab7dcc",
"metadata": {},
"outputs": [
@ -79,9 +79,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"BEFORE <function inner at 0x1038697e0>\n",
"inner function\n",
"AFTER <function inner at 0x1038697e0>\n"
"BEFORE <function inner at 0x103591240>\n",
"inner function 1 2 3\n",
"AFTER <function inner at 0x103591240>\n"
]
}
],
@ -89,12 +89,12 @@
"# often we want to just replace the function altogether\n",
"# with the modified version\n",
"inner = print_before_and_after(inner)\n",
"inner()"
"inner(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"id": "508bbf73-1f9f-41dd-9665-549472056211",
"metadata": {},
"outputs": [],
@ -104,12 +104,12 @@
"@print_before_and_after\n",
"def add_nums(a, b, c):\n",
" print(f\"{a} + {b} + {c} =\", a + b + c)\n",
"#add_nums = print_before_and_after(print_before_and_after(add_nums))"
"# add_nums = print_before_and_after(add_nums)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"id": "86ee63a9-ac5d-44f5-a06a-a89527358141",
"metadata": {},
"outputs": [
@ -117,11 +117,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"BEFORE <function print_before_and_after.<locals>.newfunc at 0x10386a0e0>\n",
"BEFORE <function add_nums at 0x10386a050>\n",
"BEFORE <function print_before_and_after.<locals>.newfunc at 0x103591ea0>\n",
"BEFORE <function add_nums at 0x1035923b0>\n",
"1 + 2 + 3 = 6\n",
"AFTER <function add_nums at 0x10386a050>\n",
"AFTER <function print_before_and_after.<locals>.newfunc at 0x10386a0e0>\n"
"AFTER <function add_nums at 0x1035923b0>\n",
"AFTER <function print_before_and_after.<locals>.newfunc at 0x103591ea0>\n"
]
}
],
@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "1e9d6428-8f80-4707-b508-c15878459d1f",
"metadata": {},
"outputs": [],
@ -150,13 +150,13 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "c50faf15-f5dc-4a1b-a114-c606a0edd0be",
"metadata": {},
"outputs": [],
"source": [
"@cache\n",
"def expensive_calculation(a, b):\n",
"def expensive_calculation(a, b, *, c=0):\n",
" print(f\"doing expensive calculation on {a} {b}...\")\n",
" return a ** b\n",
"\n",
@ -168,156 +168,69 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "dadaf583-82fd-4273-870e-7d68ec67c537",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing expensive calculation on 4 10...\n"
]
},
{
"data": {
"text/plain": [
"1048576"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"expensive_calculation(4, 10)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "6caa9b63-bc83-44c7-b416-1b5f9c708469",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1048576"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"expensive_calculation(4, 10)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "c6e9a399-9c52-4b38-8fe0-427e0843804a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing cheap calculation on 4 10...\n"
]
},
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"cheap_calculation(4, 10)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"id": "8ed9065a-4fb1-4486-ab02-b858d031eb93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing expensive calculation on 5 6...\n"
]
},
{
"data": {
"text/plain": [
"15625"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"expensive_calculation(5, 6)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"id": "975f7f9e-74c4-4dcf-b396-ad85556e930a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"cheap_calculation(4, 10)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "d3d8c310-4fa6-4b5e-b6e7-e0512adfe8c0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15625"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"expensive_calculation(5, 6)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"id": "b6da10be-562c-4968-86ed-c473db63eccf",
"metadata": {},
"outputs": [],
@ -335,49 +248,20 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"id": "7f43e771-92d9-48d3-96ed-b95b1c94e942",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3\n",
"1 2 3\n",
"1 2 3\n",
"1 2 3\n",
"1 2 3\n"
]
}
],
"outputs": [],
"source": [
"print_sum(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"id": "4327b95d-b9ab-49e3-a530-b34e0688cff6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n",
"backwards\n"
]
}
],
"outputs": [],
"source": [
"# to make a decorator that takes additional arguments\n",
"# you need to write a decorator factory function that returns decorators\n",
@ -399,18 +283,10 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"id": "8b4219ac-7326-47e1-b3ad-68941349db1d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<function repeat.<locals>.repeat_decorator at 0x1038a95a0>\n"
]
}
],
"outputs": [],
"source": [
"repeat_10 = repeat(10)\n",
"print(repeat_10)\n",
@ -419,7 +295,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"id": "a1d27a5c-ce00-4023-8efd-1900b6f876e5",
"metadata": {},
"outputs": [],
@ -430,7 +306,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"id": "fb9e74b4-a0a2-4463-add7-45b04430edcd",
"metadata": {},
"outputs": [],
@ -441,88 +317,47 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"id": "e64ec996-4085-4c03-a771-6c1073b04990",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Scott, Paul, Lauren\n"
]
}
],
"outputs": [],
"source": [
"print_hello_names(\"Scott\", \"Paul\", \"Lauren\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": null,
"id": "eb0d1ad3-e636-4430-9139-265740adc8a1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Hello',)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names.args"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"id": "9e67124b-9014-49d9-af86-54b8a617521f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sep': ', '}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names.keywords"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "f4939684-6c92-4c07-8d88-f7c4349d0f02",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function print>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names.func"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"id": "1ed27205-7691-46ee-bdc6-f75dae7d53da",
"metadata": {},
"outputs": [],
@ -538,21 +373,10 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"id": "8e02bb23-7278-4799-85d6-9004af65565b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# property is assigned to all wrapped functions\n",
"@wrapper\n",
@ -564,7 +388,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"id": "93b1ad42-b4a2-4947-a4bb-19c818968e2c",
"metadata": {},
"outputs": [],
@ -582,18 +406,10 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"id": "092b1a1d-4300-4895-aa06-4d874bc61159",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Scott, Paul, Lauren\n"
]
}
],
"outputs": [],
"source": [
"print_hello_names2 = our_partial(print, \"Hello\", sep=\", \")\n",
"print_hello_names2(\"Scott\", \"Paul\", \"Lauren\")"
@ -601,63 +417,30 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"id": "c48a5dfb-0444-4f5e-a4bc-39f0770ff5a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Hello',)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names2.args"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"id": "f5d68812-918b-42dd-b23f-925a692668aa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sep': ', '}"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names2.keywords"
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"id": "6b41b480-43e1-49b2-ade7-44d8aa484a09",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function print>"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"print_hello_names2.func"
]