Created
January 1, 2021 04:44
-
-
Save lordpretzel/55bfee9a0655ea8d12b0705411ba68be to your computer and use it in GitHub Desktop.
This file contains 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": { | |
"state": "normal" | |
}, | |
"source": [ | |
"# Python Language Intro (Part 2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"## Agenda\n", | |
"\n", | |
"1. Language overview\n", | |
"2. White space sensitivity\n", | |
"3. Basic Types and Operations\n", | |
"4. Statements & Control Structures\n", | |
"5. Functions\n", | |
"6. OOP (Classes, Methods, etc.)\n", | |
"7. Immutable Sequence Types (Strings, Ranges, Tuples)\n", | |
"8. Mutable data structures: Lists, Sets, Dictionaries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# by default, only the result of the last expression in a cell is displayed after evaluation.\n", | |
"# the following forces display of *all* self-standing expressions in a cell.\n", | |
"\n", | |
"from IPython.core.interactiveshell import InteractiveShell\n", | |
"InteractiveShell.ast_node_interactivity = \"all\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"## 4. Statements & Control Structures" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### Assignment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# simple, single target assignment\n", | |
"\n", | |
"a = 0\n", | |
"b = 'hello'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# can also assign to target \"lists\"\n", | |
"\n", | |
"a, b, c = 0, 'hello', True" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# note: expression on right is fully evaluated, then are assigned to\n", | |
"# elements in the \"target\" list, from left to right\n", | |
"\n", | |
"x, y, z = 1, 2, 3\n", | |
"x, y, z = x+y, y+z, x+y+z" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# easy python \"swap\"\n", | |
"\n", | |
"a, b = 'apples', 'bananas'\n", | |
"a, b = b, a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# note: order matters!\n", | |
"\n", | |
"a, b, a = 1, 2, 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"# can also have multiple assignments in a row -- consistent with\n", | |
"# above: expression is evaluated first, then assigned to all targets\n", | |
"# from left to right (note: order matters!)\n", | |
"\n", | |
"x = y = z = None" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### Augmented assignment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"a = 0\n", | |
"a += 2\n", | |
"a *= 3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### `pass`" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"**`pass`** is the \"do nothing\" statement" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def foo():\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### `if`-`else` statements" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"from random import randint\n", | |
"score = randint(50, 100)\n", | |
"grade = None\n", | |
"if score >= 90:\n", | |
" grade = 'A'\n", | |
"elif score >= 80:\n", | |
" grade = 'B'\n", | |
"elif score >= 70:\n", | |
" grade = 'C'\n", | |
"elif score >= 60:\n", | |
" grade = 'D'\n", | |
"else:\n", | |
" grade = 'E'\n", | |
"\n", | |
"print(score, grade)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### `while` loops" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f0 = 0\n", | |
"f1 = 1\n", | |
"while f0 < 100:\n", | |
" print(f0)\n", | |
" f0, f1 = f1, f0+f1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"i = 0\n", | |
"to_find = 10\n", | |
"while i < 5:\n", | |
" i += 1\n", | |
" if i == to_find:\n", | |
" print('Found; breaking early')\n", | |
" break\n", | |
"else:\n", | |
" print('Not found; terminated loop')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"i = 0\n", | |
"to_find = 10\n", | |
"while i < 100:\n", | |
" i += 1\n", | |
" if i == to_find:\n", | |
" print('Found; breaking early')\n", | |
" break\n", | |
"else:\n", | |
" print('Not found; terminated loop')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### Exception Handling" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"raise Exception('Boom!')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"raise NotImplementedError()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" raise Exception('Boom')\n", | |
"except:\n", | |
" print('Exception encountered!')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" raise ArithmeticError('Eeek!')\n", | |
"except LookupError as e:\n", | |
" print('LookupError:', e)\n", | |
"except ArithmeticError as e:\n", | |
" print('ArithmeticError:', e)\n", | |
"except Exception as e:\n", | |
" print(e)\n", | |
"finally:\n", | |
" print('Done')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### `for` loops (iteration)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"for x in range(10):\n", | |
" print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"for i in range(9, 81, 9):\n", | |
" print(i)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"for c in 'hello world':\n", | |
" print(c)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"to_find = 50\n", | |
"for i in range(100):\n", | |
" if i == to_find:\n", | |
" break\n", | |
"else:\n", | |
" print('Completed loop')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### Generalized iteration (`iter` and `next`)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"r = range(10)\n", | |
"it = iter(r)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"type(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"it = iter(r)\n", | |
"while True:\n", | |
" try:\n", | |
" x = next(it)\n", | |
" print(x)\n", | |
" except StopIteration:\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"it = iter(r)\n", | |
"while True:\n", | |
" try:\n", | |
" x = next(it)\n", | |
" y = next(it)\n", | |
" print(x, y, x+y)\n", | |
" except StopIteration:\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"## 5. Functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def foo():\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"import math\n", | |
"\n", | |
"def quadratic_roots(a, b, c):\n", | |
" disc = b**2-4*a*c\n", | |
" if disc < 0:\n", | |
" return None\n", | |
" else:\n", | |
" return (-b+math.sqrt(disc))/(2*a), (-b-math.sqrt(disc))/(2*a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"quadratic_roots(1, -5, 6) # eq = (x-3)(x-2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"quadratic_roots(a=1, b=-5, c=6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"quadratic_roots(c=6, a=1, b=-5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def create_character(name, race, hitpoints, ability):\n", | |
" print('Name:', name)\n", | |
" print('Race:', race)\n", | |
" print('Hitpoints:', hitpoints)\n", | |
" print('Ability:', ability)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Legolas', 'Elf', 100, 'Archery')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def create_character(name, race='Human', hitpoints=100, ability=None):\n", | |
" print('Name:', name)\n", | |
" print('Race:', race)\n", | |
" print('Hitpoints:', hitpoints)\n", | |
" if ability:\n", | |
" print('Ability:', ability)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Michael')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def create_character(name, race='Human', hitpoints=100, abilities=()):\n", | |
" print('Name:', name)\n", | |
" print('Race:', race)\n", | |
" print('Hitpoints:', hitpoints)\n", | |
" if abilities:\n", | |
" print('Abilities:')\n", | |
" for ability in abilities:\n", | |
" print(' -', ability)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Gimli', race='Dwarf')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Gandalf', hitpoints=1000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Aragorn', abilities=('Swording', 'Healing'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def create_character(name, *abilities, race='Human', hitpoints=100):\n", | |
" print('Name:', name)\n", | |
" print('Race:', race)\n", | |
" print('Hitpoints:', hitpoints)\n", | |
" if abilities:\n", | |
" print('Abilities:')\n", | |
" for ability in abilities:\n", | |
" print(' -', ability)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Michael')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"create_character('Michael', 'Coding', 'Teaching', 'Sleeping', hitpoints=25, )" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"### Functions as Objects" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def foo():\n", | |
" print('Foo called')\n", | |
" \n", | |
"bar = foo\n", | |
"bar()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def foo(f):\n", | |
" f()\n", | |
" \n", | |
"def bar():\n", | |
" print('Bar called')\n", | |
" \n", | |
"foo(bar)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"foo = lambda: print('Anonymous function called')\n", | |
"\n", | |
"foo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f = lambda x,y: x+y\n", | |
"\n", | |
"f(1,2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def my_map(f, it):\n", | |
" for x in it:\n", | |
" print(f(x))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"my_map(lambda x: x*2, range(1,10))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"for x in map(lambda x: x*2, range(1,10)):\n", | |
" print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"def foo():\n", | |
" print('Foo called')\n", | |
"\n", | |
"type(foo)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"dir(foo)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"foo.__call__()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"state": "normal" | |
}, | |
"source": [ | |
"## 6. OOP (Classes, Methods, etc.)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Foo:\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"type(Foo)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"Foo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"type(Foo())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"__name__ # name of the current \"module\" (for this notebook)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"globals().keys() # symbol table of the current module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"m = sys.modules['__main__'] # explicitly accessing the __main__b module\n", | |
"dir(m)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"m.Foo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f = Foo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f.x = 100\n", | |
"f.y = 50\n", | |
"f.x + f.y" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"g = Foo()\n", | |
"g.x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Foo:\n", | |
" def bar():\n", | |
" print('Bar called')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"type(Foo.bar)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f = Foo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"type(f.bar)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"Foo.bar()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true, | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f.bar()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Foo:\n", | |
" def bar(x):\n", | |
" print('Bar called with', x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"Foo.bar()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f = Foo()\n", | |
"f.bar()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Foo:\n", | |
" def bar(self):\n", | |
" self.x = 'Some value'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"f = Foo()\n", | |
"f.bar()\n", | |
"f.x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Shape:\n", | |
" def __init__(self, name):\n", | |
" self.name = name\n", | |
" \n", | |
" def __repr__(self):\n", | |
" return self.name\n", | |
" \n", | |
" def __str__(self):\n", | |
" return self.name.upper()\n", | |
" \n", | |
" def area(self):\n", | |
" raise NotImplementedError()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"s = Shape('circle')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"s" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"str(s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"s.area()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Circle(Shape):\n", | |
" def __init__(self, radius):\n", | |
" super().__init__('circle')\n", | |
" self.radius = radius\n", | |
" \n", | |
" def area(self):\n", | |
" return 3.14 * self.radius ** 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"c = Circle(5.0)\n", | |
"c\n", | |
"c.area()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Circle(Shape):\n", | |
" def __init__(self, radius):\n", | |
" super().__init__('circle')\n", | |
" self.radius = radius\n", | |
" \n", | |
" def area(self):\n", | |
" return 3.14 * self.radius ** 2\n", | |
" \n", | |
" def __eq__(self, other):\n", | |
" return isinstance(other, Circle) and self.radius == other.radius\n", | |
" \n", | |
" def __add__(self, other):\n", | |
" return Circle(self.radius + other.radius)\n", | |
" \n", | |
" def __repr__(self):\n", | |
" return 'Circle(r={})'.format(self.radius)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"state": "normal" | |
}, | |
"outputs": [], | |
"source": [ | |
"c1 = Circle(2.0)\n", | |
"c2 = Circle(4.0)\n", | |
"c3 = Circle(2.0)\n", | |
"\n", | |
"c1, c2, c3\n", | |
"c1 == c2\n", | |
"c1 == c3\n", | |
"c1 + c2" | |
] | |
} | |
], | |
"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.3" | |
}, | |
"mimir": { | |
"data": {}, | |
"last_submission_id": "", | |
"project_id": "dd0defe7-3363-4a9f-8119-5a4542b3b73c" | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment