Created
March 12, 2021 11:44
-
-
Save fxadecimal/0dd875a562688d249294a878c8ffe517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "comparative-processing", | |
| "metadata": {}, | |
| "source": [ | |
| "# The magic of Yield" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "accompanied-dubai", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import pprint\n", | |
| "pp = pprint.PrettyPrinter(indent=4)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "horizontal-albany", | |
| "metadata": {}, | |
| "source": [ | |
| "## Iterators \n", | |
| "\n", | |
| "An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.\n", | |
| "\n", | |
| "[https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html]()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "color-niger", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "2\n", | |
| "3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "l = list([1,2,3])\n", | |
| "\n", | |
| "for item in iter(l):\n", | |
| " print( item )\n", | |
| " \n", | |
| "\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "regulated-typing", | |
| "metadata": {}, | |
| "source": [ | |
| "## Generators\n", | |
| "\n", | |
| "- Either a Generator function that contains the `Yield` function\n", | |
| "- Generator class implementing the python magic methods `__iter__()` or `__next__()`\n", | |
| "- With `__iter__()`, returning something iteratable, that implements the `__next__()` method\n", | |
| "\n", | |
| "\n", | |
| "References\n", | |
| "- [https://wiki.python.org/moin/Generators](https://wiki.python.org/moin/Generators)\n", | |
| "- [https://www.programiz.com/python-programming/generator](https://www.programiz.com/python-programming/generator)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "hearing-bubble", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "<class 'range'>\n", | |
| "[ '__bool__',\n", | |
| " '__class__',\n", | |
| " '__contains__',\n", | |
| " '__delattr__',\n", | |
| " '__dir__',\n", | |
| " '__doc__',\n", | |
| " '__eq__',\n", | |
| " '__format__',\n", | |
| " '__ge__',\n", | |
| " '__getattribute__',\n", | |
| " '__getitem__',\n", | |
| " '__gt__',\n", | |
| " '__hash__',\n", | |
| " '__init__',\n", | |
| " '__init_subclass__',\n", | |
| " '__iter__',\n", | |
| " '__le__',\n", | |
| " '__len__',\n", | |
| " '__lt__',\n", | |
| " '__ne__',\n", | |
| " '__new__',\n", | |
| " '__reduce__',\n", | |
| " '__reduce_ex__',\n", | |
| " '__repr__',\n", | |
| " '__reversed__',\n", | |
| " '__setattr__',\n", | |
| " '__sizeof__',\n", | |
| " '__str__',\n", | |
| " '__subclasshook__',\n", | |
| " 'count',\n", | |
| " 'index',\n", | |
| " 'start',\n", | |
| " 'step',\n", | |
| " 'stop']\n", | |
| "<range_iterator object at 0x1032623f0>\n", | |
| "0\n", | |
| "1\n", | |
| "2\n", | |
| "3\n", | |
| "4\n", | |
| "5\n", | |
| "6\n", | |
| "7\n", | |
| "8\n", | |
| "9\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# range was formally xrange in python2\n", | |
| "\n", | |
| "l = range(10) # range generator\n", | |
| "\n", | |
| "\n", | |
| "# what is the python class? range!\n", | |
| "print(type(l))\n", | |
| "\n", | |
| "# what are it's methods?\n", | |
| "pp.pprint(dir(l)) # Note the __iter__ method\n", | |
| "\n", | |
| "\n", | |
| "print( iter(l) ) # gives us a range_iterator to iterate over the range object\n", | |
| "\n", | |
| "for i in iter(l):\n", | |
| " print(i)\n", | |
| "\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "breeding-connectivity", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0\n", | |
| "10\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def generator_function(initial_value = 0):\n", | |
| " num = initial_value\n", | |
| " while True:\n", | |
| " yield num # < note the yield\n", | |
| " num += 10\n", | |
| " \n", | |
| "gen = generator_function()\n", | |
| "print( next(gen) )\n", | |
| "print( next(gen) )\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "boxed-millennium", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "2\n", | |
| "4\n", | |
| "8\n", | |
| "16\n", | |
| "32\n", | |
| "64\n", | |
| "128\n", | |
| "256\n", | |
| "512\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# generator class\n", | |
| "\n", | |
| "class PowTwo:\n", | |
| " def __init__(self, max=0):\n", | |
| " self.n = 0\n", | |
| " self.max = max\n", | |
| "\n", | |
| " def __iter__(self):\n", | |
| " return self\n", | |
| "\n", | |
| " def __next__(self):\n", | |
| " if self.n > self.max:\n", | |
| " raise StopIteration\n", | |
| "\n", | |
| " result = 2 ** self.n\n", | |
| " self.n += 1\n", | |
| " return result\n", | |
| " \n", | |
| " \n", | |
| "powtwo = PowTwo( 10 )\n", | |
| "powtwo_iteratable = iter(powtwo) # calls __iter__()\n", | |
| "\n", | |
| "for i in range(0,10):\n", | |
| " print( next(powtwo) ) # changes state of powtwo\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "designing-crowd", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "2\n", | |
| "4\n", | |
| "8\n", | |
| "16\n", | |
| "32\n", | |
| "64\n", | |
| "128\n", | |
| "256\n", | |
| "512\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# simplify the generator class to a generator function\n", | |
| "\n", | |
| "def PowTwoGen(max=0):\n", | |
| " n = 0\n", | |
| " while n < max:\n", | |
| " yield 2 ** n\n", | |
| " n += 1\n", | |
| "\n", | |
| " \n", | |
| "powtwo_iteratable = PowTwoGen(10) \n", | |
| "for i in range(0,10):\n", | |
| " print( next(powtwo_iteratable) ) " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "dirty-airfare", | |
| "metadata": {}, | |
| "source": [ | |
| "## Yield\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "adequate-slave", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "10\n", | |
| "20\n", | |
| "30\n", | |
| "40\n", | |
| "50\n", | |
| "60\n", | |
| "70\n", | |
| "80\n", | |
| "90\n", | |
| "100\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "\n", | |
| "\n", | |
| "def generator_function(initial_value = 10):\n", | |
| " num = initial_value\n", | |
| " while True:\n", | |
| " yield num # < note the yield\n", | |
| " num += 10\n", | |
| "\n", | |
| "gen = generator_function() \n", | |
| "for i in range(0,10):\n", | |
| " print( next(gen) ) # generator doesn't get resolved until we hit next\n", | |
| " " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "valuable-fishing", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.9.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment