from pres

This commit is contained in:
James Turk 2024-11-08 22:22:09 -06:00
parent 4a0ac4a74b
commit 53ee3577a1
3 changed files with 412 additions and 129 deletions

View File

@ -235,18 +235,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 5,
"id": "5022b7c8-a19f-45cb-9d40-58abc48de961", "id": "5022b7c8-a19f-45cb-9d40-58abc48de961",
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
"outputs": [ "outputs": [
{ {
"ename": "SyntaxError", "ename": "NameError",
"evalue": "expected 'except' or 'finally' block (572013031.py, line 6)", "evalue": "name 'd' is not defined",
"output_type": "error", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[10], line 6\u001b[0;36m\u001b[0m\n\u001b[0;31m print(\"hello\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m expected 'except' or 'finally' block\n" "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[5], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#c = a / b\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43md\u001b[49m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlast line of try\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m:\n",
"\u001b[0;31mNameError\u001b[0m: name 'd' is not defined"
] ]
} }
], ],
@ -254,11 +257,14 @@
"try:\n", "try:\n",
" a = 3\n", " a = 3\n",
" b = 0\n", " b = 0\n",
" c = a / b\n", " #c = a / b\n",
" print(d)\n",
" print(\"last line of try\")\n", " print(\"last line of try\")\n",
"except ZeroDivisionError:\n", "except ZeroDivisionError:\n",
" c = 0\n", " c = 0\n",
" print(f\"can't divide by zero, set c = {c}\")\n", " print(f\"can't divide by zero, set c = {c}\")\n",
"#except NameError:\n",
"# print(\"used unknown name\")\n",
"print(\"and then this happens\")" "print(\"and then this happens\")"
] ]
}, },
@ -274,7 +280,7 @@
"```python\n", "```python\n",
"# multiple except blocks for different behaviors\n", "# multiple except blocks for different behaviors\n",
"try:\n", "try:\n",
" something():\n", " something()\n",
"except ValueError:\n", "except ValueError:\n",
" ...\n", " ...\n",
"except IndexError:\n", "except IndexError:\n",
@ -284,7 +290,7 @@
"```python\n", "```python\n",
"# one block w/ multiple types in a tuple\n", "# one block w/ multiple types in a tuple\n",
"try:\n", "try:\n",
" something()\n", " somthing()\n",
"except (ValueError, IndexError, KeyError): # tuple of return values\n", "except (ValueError, IndexError, KeyError): # tuple of return values\n",
" ...\n", " ...\n",
"```\n", "```\n",
@ -293,9 +299,10 @@
"# base exception type, use sparingly\n", "# base exception type, use sparingly\n",
"try:\n", "try:\n",
" something()\n", " something()\n",
"except ArithmeticError: # catches all errors derived from ArithmeticError\n",
" ...\n",
"except Exception: # catches all runtime exceptions\n", "except Exception: # catches all runtime exceptions\n",
" ...\n", " ...\n",
"except ArithmeticError: # catches all errors derived from ArithmeticError\n",
"```\n", "```\n",
"\n", "\n",
"\n", "\n",
@ -328,7 +335,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 7,
"id": "fd6d0e12-eae2-466e-8536-db5d246091f0", "id": "fd6d0e12-eae2-466e-8536-db5d246091f0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -341,7 +348,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 20,
"id": "58c691a9-14ed-4fe1-9b0d-eb9b52a4d44d", "id": "58c691a9-14ed-4fe1-9b0d-eb9b52a4d44d",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -351,7 +358,7 @@
"9" "9"
] ]
}, },
"execution_count": 10, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -362,25 +369,42 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 27,
"id": "5fd8e7ee-53c6-4cd1-8748-cd66f06da9ac", "id": "5fd8e7ee-53c6-4cd1-8748-cd66f06da9ac",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "ValueError", "name": "stdout",
"evalue": "f requires a positive argument", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "got an error: f requires a positive argument\n"
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[9], line 3\u001b[0m, in \u001b[0;36mf\u001b[0;34m(positive)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf\u001b[39m(positive):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m positive \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mf requires a positive argument\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m positive \u001b[38;5;241m*\u001b[39m positive\n",
"\u001b[0;31mValueError\u001b[0m: f requires a positive argument"
] ]
} }
], ],
"source": [ "source": [
"f(-1)" "try:\n",
" y = f(-1)\n",
"except ValueError as exc:\n",
" y = 0\n",
" print(\"got an error: \", exc)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "769fb253-ae47-4e47-a6fd-9a37e1ef3d5e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"print(y)"
] ]
}, },
{ {
@ -404,6 +428,7 @@
"source": [ "source": [
"class InvalidColor(Exception):\n", "class InvalidColor(Exception):\n",
" \"\"\" This exception is raised when an invalid color is passed. \"\"\"\n", " \"\"\" This exception is raised when an invalid color is passed. \"\"\"\n",
" pass\n",
"\n", "\n",
"VALID_COLORS = (...)\n", "VALID_COLORS = (...)\n",
"\n", "\n",
@ -448,7 +473,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 30,
"id": "320d3309-099a-4f2e-9096-983101bfc157", "id": "320d3309-099a-4f2e-9096-983101bfc157",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -456,21 +481,22 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"got a value error error message\n", "else executed\n",
"always prints at the end\n" "always prints at the end\n"
] ]
} }
], ],
"source": [ "source": [
"try:\n", "try:\n",
" #pass\n", " 1 / 2\n",
" raise ValueError(\"error message\")\n",
"except ValueError as e:\n", "except ValueError as e:\n",
" print(\"got a value error\", e)\n", " print(\"got a value error:\", e)\n",
"except Exception as e:\n",
" print(\"got some other error:\", type(e), e)\n",
"else:\n", "else:\n",
" print(\"no exception raised\")\n", " print(\"else executed\")\n",
"finally:\n", "finally:\n",
" print(\"always prints at the end\")" " print(\"always prints at the end\")\n"
] ]
}, },
{ {

View File

@ -14,7 +14,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 1,
"id": "feaa69a2", "id": "feaa69a2",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -38,6 +38,7 @@
" self.year = 1\n", " self.year = 1\n",
" self.major = \"Undeclared\"\n", " self.major = \"Undeclared\"\n",
" self.course_grades = {}\n", " self.course_grades = {}\n",
" self.extracurriculars = []\n",
" \n", " \n",
" def add_grade(self, course_name, grade):\n", " def add_grade(self, course_name, grade):\n",
" self.course_grades[course_name] = grade\n", " self.course_grades[course_name] = grade\n",
@ -55,7 +56,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 45,
"id": "c410fb1d", "id": "c410fb1d",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -70,7 +71,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 46,
"id": "f9deab26", "id": "f9deab26",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -80,8 +81,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Student(name=Adam, id=1, gpa=0)\n", "Student(name=Adam, id=8, gpa=0)\n",
"Student(name=Beth, id=2, gpa=3.65)\n" "Student(name=Beth, id=9, gpa=3.65)\n"
] ]
} }
], ],
@ -168,7 +169,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 49,
"id": "47f7dfc9", "id": "47f7dfc9",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -187,7 +188,8 @@
" \n", " \n",
" # overrides parent's add_grade\n", " # overrides parent's add_grade\n",
" def add_grade(self, course_name, grade):\n", " def add_grade(self, course_name, grade):\n",
" print(\"Sorry, you cannot add grades to Alums\")\n", " raise NotImplementedError(\"cannot add grades to Alum\")\n",
" #print(\"Sorry, you cannot add grades to Alums\")\n",
" # we choose not call super().add_grade here\n", " # we choose not call super().add_grade here\n",
" \n", " \n",
" # overrides parent's __repr__\n", " # overrides parent's __repr__\n",
@ -200,7 +202,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 51,
"id": "4811b0fd", "id": "4811b0fd",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -210,9 +212,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Student(name=Charlie, id=3, gpa=0) is an alum\n", "Student(name=Charlie, id=13, gpa=0) is an alum\n",
"6 years since graduation\n", "6 years since graduation\n"
"Sorry, you cannot add grades to Alums\n"
] ]
}, },
{ {
@ -221,7 +222,7 @@
"0" "0"
] ]
}, },
"execution_count": 13, "execution_count": 51,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -230,13 +231,13 @@
"alum1 = Alum(\"Charlie\", 2016)\n", "alum1 = Alum(\"Charlie\", 2016)\n",
"print(alum1)\n", "print(alum1)\n",
"print(alum1.years_since_graduation(2022), \"years since graduation\")\n", "print(alum1.years_since_graduation(2022), \"years since graduation\")\n",
"alum1.add_grade(\"Python\", \"B\")\n", "#alum1.add_grade(\"Python\", \"B\")\n",
"alum1.gpa" "alum1.gpa"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 54,
"id": "18a75666-9986-4e2b-9d55-ec0e0b9e930d", "id": "18a75666-9986-4e2b-9d55-ec0e0b9e930d",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -248,13 +249,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 56,
"id": "b6719a36-b827-4373-87e0-23714438b83f", "id": "b6719a36-b827-4373-87e0-23714438b83f",
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
"outputs": [], "outputs": [
"source": [] {
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'Alum' and 'Alum'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[56], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43malum1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43malum2\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Alum' and 'Alum'"
]
}
],
"source": [
"alum1 + alum2"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -278,7 +293,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 8,
"id": "a46d5b02", "id": "a46d5b02",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -290,7 +305,7 @@
"True" "True"
] ]
}, },
"execution_count": 13, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -301,12 +316,23 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 9,
"id": "d3596a45-2c00-4541-910d-08251d3f9c5f", "id": "d3596a45-2c00-4541-910d-08251d3f9c5f",
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# same as?\n", "# same as?\n",
"type(7) == int" "type(7) == int"
@ -314,7 +340,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 10,
"id": "eaa8b6db-e7df-4137-b4c8-67dfa0be06d5", "id": "eaa8b6db-e7df-4137-b4c8-67dfa0be06d5",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -324,7 +350,7 @@
"False" "False"
] ]
}, },
"execution_count": 6, "execution_count": 10,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -335,7 +361,28 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 57,
"id": "9cfc8480-ecca-4f80-9dfe-70a655bc5b3c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(7, object)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "0e50f7bc-541e-4fec-9cd6-ad708f312831", "id": "0e50f7bc-541e-4fec-9cd6-ad708f312831",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -345,32 +392,75 @@
"True" "True"
] ]
}, },
"execution_count": 7, "execution_count": 62,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"# isinstance checks the inheritance hierarchy \n", "# isinstance checks the inheritance hierarchy \n",
"isinstance(7, object)" "isinstance(alum2, object)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 64,
"id": "34e491d5-ac5c-4dc9-b059-491e0f0f73b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(alum2) == Student"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "cd55fd8d", "id": "cd55fd8d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"isinstance([1, 2, 3], list)" "isinstance([1, 2, 3], list)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"id": "feb9f2ac", "id": "feb9f2ac",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"s1 = Student(\"Sarah\")\n", "s1 = Student(\"Sarah\")\n",
"isinstance(s1, Student)" "isinstance(s1, Student)"
@ -378,10 +468,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 18,
"id": "e6a2f3b1", "id": "e6a2f3b1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# child classes are instances of parent types\n", "# child classes are instances of parent types\n",
"alum1 = Alum(\"Charlie\", 2016)\n", "alum1 = Alum(\"Charlie\", 2016)\n",
@ -390,10 +491,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 19,
"id": "8f759efb", "id": "8f759efb",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# but not vice-versa\n", "# but not vice-versa\n",
"isinstance(s1, Alum)" "isinstance(s1, Alum)"
@ -401,7 +513,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 20,
"id": "1d52b927", "id": "1d52b927",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -414,7 +526,7 @@
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[15], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# takes class names\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;43missubclass\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43malum1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mStudent\u001b[49m\u001b[43m)\u001b[49m\n", "Cell \u001b[0;32mIn[20], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# takes class names\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;43missubclass\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43malum1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mStudent\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: issubclass() arg 1 must be a class" "\u001b[0;31mTypeError\u001b[0m: issubclass() arg 1 must be a class"
] ]
} }
@ -426,7 +538,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 22,
"id": "a71ee1cb", "id": "a71ee1cb",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -435,16 +547,16 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"False" "True"
] ]
}, },
"execution_count": 16, "execution_count": 22,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"issubclass(Student, Alum)" "issubclass(Alum, Student)"
] ]
}, },
{ {
@ -478,7 +590,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 66,
"id": "1d4e00ca", "id": "1d4e00ca",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -488,7 +600,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Help on Alum in module __main__ object:\n", "Help on class Alum in module __main__:\n",
"\n", "\n",
"class Alum(Student)\n", "class Alum(Student)\n",
" | Alum(name, grad_year)\n", " | Alum(name, grad_year)\n",
@ -529,13 +641,100 @@
" | ----------------------------------------------------------------------\n", " | ----------------------------------------------------------------------\n",
" | Data and other attributes inherited from Student:\n", " | Data and other attributes inherited from Student:\n",
" | \n", " | \n",
" | next_id_counter = 5\n", " | next_id_counter = 16\n",
"\n" "\n"
] ]
} }
], ],
"source": [ "source": [
"help(alum1)" "help(Alum)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "45f732f0-2036-416e-a44c-70b99b18a960",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(__main__.Alum, __main__.Student, object)"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Alum.__mro__"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "592f6930-cf36-4ec1-a4f3-ea1947fa7173",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class super in module builtins:\n",
"\n",
"class super(object)\n",
" | super() -> same as super(__class__, <first argument>)\n",
" | super(type) -> unbound super object\n",
" | super(type, obj) -> bound super object; requires isinstance(obj, type)\n",
" | super(type, type2) -> bound super object; requires issubclass(type2, type)\n",
" | Typical use to call a cooperative superclass method:\n",
" | class C(B):\n",
" | def meth(self, arg):\n",
" | super().meth(arg)\n",
" | This works for class methods too:\n",
" | class C(B):\n",
" | @classmethod\n",
" | def cmeth(cls, arg):\n",
" | super().cmeth(arg)\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __get__(self, instance, owner=None, /)\n",
" | Return an attribute of instance, which is of type owner.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __init__(self, /, *args, **kwargs)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods defined here:\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | __self__\n",
" | the instance invoking super(); may be None\n",
" | \n",
" | __self_class__\n",
" | the type of the instance invoking super(); may be None\n",
" | \n",
" | __thisclass__\n",
" | the class invoking super()\n",
"\n"
]
}
],
"source": [
"help(super)"
] ]
}, },
{ {
@ -584,12 +783,18 @@
" def __init__(self,x,y):\n", " def __init__(self,x,y):\n",
" self.x = x\n", " self.x = x\n",
" self.y = y \n", " self.y = y \n",
"\n",
" def dot_product(self, other):\n",
" ...\n",
" \n", " \n",
"class Vec3:\n", "class Vec3:\n",
" def __init__(self,x,y,z):\n", " def __init__(self,x,y,z):\n",
" self.x = x\n", " self.x = x\n",
" self.y = y \n", " self.y = y \n",
" self.z = z " " self.z = z \n",
"\n",
" def dot(self, other):\n",
" ..."
] ]
}, },
{ {
@ -606,7 +811,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 72,
"id": "a2cddb6d", "id": "a2cddb6d",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -615,7 +820,10 @@
"source": [ "source": [
"from abc import ABC, abstractmethod\n", "from abc import ABC, abstractmethod\n",
"\n", "\n",
"class Vector(ABC): \n", "class Vector(ABC): \n",
" def print_x(self):\n",
" print(self.x)\n",
" \n",
" @abstractmethod\n", " @abstractmethod\n",
" def dot_product(self, other):\n", " def dot_product(self, other):\n",
" pass" " pass"
@ -623,7 +831,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 44, "execution_count": 71,
"id": "a32d26a8", "id": "a32d26a8",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -631,13 +839,13 @@
"outputs": [ "outputs": [
{ {
"ename": "TypeError", "ename": "TypeError",
"evalue": "Can't instantiate abstract class Vector with abstract method dot_product", "evalue": "Can't instantiate abstract class Vector with abstract methods __repr__, dot_product",
"output_type": "error", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[44], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# we can't instantiate abstract classes\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m v \u001b[38;5;241m=\u001b[39m \u001b[43mVector\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", "Cell \u001b[0;32mIn[71], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# we can't instantiate abstract classes\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m v \u001b[38;5;241m=\u001b[39m \u001b[43mVector\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class Vector with abstract method dot_product" "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class Vector with abstract methods __repr__, dot_product"
] ]
} }
], ],
@ -648,7 +856,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": 75,
"id": "5a9ae047", "id": "5a9ae047",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -675,7 +883,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 76,
"id": "e25536d1", "id": "e25536d1",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -710,6 +918,24 @@
"print(isinstance(v3a, Vector))" "print(isinstance(v3a, Vector))"
] ]
}, },
{
"cell_type": "code",
"execution_count": 78,
"id": "69ac4241-b251-4485-9ba9-3b0348d82c34",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"v2a.print_x()"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 47, "execution_count": 47,
@ -762,7 +988,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 53, "execution_count": 79,
"id": "4f98cf62-52da-4072-bf5a-da7de3ad9a98", "id": "4f98cf62-52da-4072-bf5a-da7de3ad9a98",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -793,7 +1019,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 54, "execution_count": 43,
"id": "8faf9c31-33e4-42f1-a8e8-8b72a7f1f96f", "id": "8faf9c31-33e4-42f1-a8e8-8b72a7f1f96f",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -803,7 +1029,29 @@
"wrench = InventoryItem(\"Wrench\", 12.95, 10)\n", "wrench = InventoryItem(\"Wrench\", 12.95, 10)\n",
"hammer = InventoryItem(\"Hammer\", 16, 8)\n", "hammer = InventoryItem(\"Hammer\", 16, 8)\n",
"nails = InventoryItem(\"Nails\", 0.03, 1000)\n", "nails = InventoryItem(\"Nails\", 0.03, 1000)\n",
"saw = InventoryItem(\"Saw\", 99)" "saw = InventoryItem(\"Saw\", 99)\n",
"saw2 = InventoryItem(\"Saw\", 99)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "94f14911-335c-4097-8658-b9a30a70d53b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"saw == saw2"
] ]
}, },
{ {
@ -884,7 +1132,9 @@
"id": "73195ac4-c0cc-4f8b-8852-2b63168e1713", "id": "73195ac4-c0cc-4f8b-8852-2b63168e1713",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"functools."
]
} }
], ],
"metadata": { "metadata": {

View File

@ -35,7 +35,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 61,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -54,14 +54,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 62,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"<__main__.StaticArray object at 0x107899060>\n" "<__main__.StaticArray object at 0x1079a50f0>\n"
] ]
} }
], ],
@ -72,7 +72,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 63,
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
@ -81,7 +81,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"<__main__.StaticArray object at 0x107899b70>\n" "<__main__.StaticArray object at 0x107b550f0>\n"
] ]
} }
], ],
@ -92,7 +92,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 64,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -126,7 +126,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 65,
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
@ -173,7 +173,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -214,7 +214,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 68,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -223,7 +223,7 @@
"4" "4"
] ]
}, },
"execution_count": 7, "execution_count": 68,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -235,7 +235,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 70,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -244,7 +244,7 @@
"True" "True"
] ]
}, },
"execution_count": 8, "execution_count": 70,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -256,27 +256,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 72,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"1" "True"
] ]
}, },
"execution_count": 9, "execution_count": 72,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"sa[0]" "sa[3]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 73,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -286,8 +286,8 @@
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43msa\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "Cell \u001b[0;32mIn[73], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43msa\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"Cell \u001b[0;32mIn[6], line 29\u001b[0m, in \u001b[0;36mStaticArray.__setitem__\u001b[0;34m(self, index, val)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__setitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, index, val):\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity \u001b[38;5;129;01mor\u001b[39;00m index \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity:\n\u001b[0;32m---> 29\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mIndexError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndex out of range\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mitems[index] \u001b[38;5;241m=\u001b[39m val\n", "Cell \u001b[0;32mIn[67], line 29\u001b[0m, in \u001b[0;36mStaticArray.__setitem__\u001b[0;34m(self, index, val)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__setitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, index, val):\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity \u001b[38;5;129;01mor\u001b[39;00m index \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity:\n\u001b[0;32m---> 29\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mIndexError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndex out of range\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mitems[index] \u001b[38;5;241m=\u001b[39m val\n",
"\u001b[0;31mIndexError\u001b[0m: Index out of range" "\u001b[0;31mIndexError\u001b[0m: Index out of range"
] ]
} }
@ -395,7 +395,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in iterable:\n",
" print(i)\n",
"\n",
"iterator = iter(iterable)\n",
"while True:\n",
" print(next(iterator))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -408,6 +422,7 @@
" return self\n", " return self\n",
"\n", "\n",
" def __next__(self):\n", " def __next__(self):\n",
" print(f\"next was called, moving {self.current} to {self.current+1}\")\n",
" if self.current >= self.n:\n", " if self.current >= self.n:\n",
" raise StopIteration\n", " raise StopIteration\n",
" else:\n", " else:\n",
@ -420,43 +435,35 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 59,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"0\n", "0 0\n",
"1\n", "0 1\n",
"2\n", "0 2\n",
"3\n", "1 0\n",
"4\n" "1 1\n",
] "1 2\n",
}, "2 0\n",
{ "2 1\n",
"ename": "StopIteration", "2 2\n"
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[13], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m sr:\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(i)\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43msr\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[12], line 11\u001b[0m, in \u001b[0;36mSimpleRange.__next__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__next__\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcurrent \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn:\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcurrent \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n",
"\u001b[0;31mStopIteration\u001b[0m: "
] ]
} }
], ],
"source": [ "source": [
"sr = SimpleRange(5)\n", "sr = range(3)\n",
"for i in sr:\n", "for i in sr:\n",
" print(i)\n", " for j in sr:\n",
"next(sr)" " print(i, j)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 57,
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
@ -537,7 +544,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Adding __iter__ to StaticArray\n", "# Adding __iter__ to StaticArray\n",
"class StaticArrayIterator:\n", "eclass StaticArrayIterator:\n",
" def __init__(self, values):\n", " def __init__(self, values):\n",
" self.values = values\n", " self.values = values\n",
" self.position = 0\n", " self.position = 0\n",