Created
December 19, 2018 14:37
-
-
Save Chrislu30604/b4e5dd33bd27755970af9652ad6336e2 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", | |
"metadata": {}, | |
"source": [ | |
"# Yield Generator" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## What 's Generator ?\n", | |
"- S generator is a function that returns an object (iterator) which we can iterate over (one value at a time).\n", | |
"\n", | |
"- Ex: class with __iter()__, __next__(), **yield**\n", | |
"\n", | |
"## Yield Feature\n", | |
"- If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function. Both yield and return will return some value from a function.\n", | |
"\n", | |
"## Yield vs Return \n", | |
"- The difference is that, while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example : Fibonacci series" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### print" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"def fab(max): \n", | |
" n, a, b = 0, 0, 1 \n", | |
" while n < max: \n", | |
" print(b) \n", | |
" a, b = b, a + b \n", | |
" n = n + 1\n", | |
"fab(5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Defect\n", | |
" 1. Bad for repetitive usage\n", | |
" 2. other variable or function cannot access the result" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### return " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"def fab(max): \n", | |
" n, a, b = 0, 0, 1 \n", | |
" L = [] \n", | |
" while n < max: \n", | |
" L.append(b) \n", | |
" a, b = b, a + b \n", | |
" n = n + 1 \n", | |
" return L\n", | |
"\n", | |
"for n in fab(5): \n", | |
" print(n) " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Defect\n", | |
" - Memory problem (if the size reach high)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Class and Generator ( implement __iter() and next)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Fab(object): \n", | |
" \n", | |
" def __init__(self, max): \n", | |
" self.max = max \n", | |
" self.n, self.a, self.b = 0, 0, 1 \n", | |
"\n", | |
" def __iter__(self): \n", | |
" return self \n", | |
"\n", | |
" def next(self): \n", | |
" if self.n < self.max: \n", | |
" r = self.b \n", | |
" self.a, self.b = self.b, self.a + self.b \n", | |
" self.n = self.n + 1 \n", | |
" return r \n", | |
" raise StopIteration()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Defect\n", | |
" - Not clear" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Yield" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"def fab(max): \n", | |
" n, a, b = 0, 0, 1 \n", | |
" while n < max: \n", | |
" yield b \n", | |
" a, b = b, a + b \n", | |
" n = n + 1\n", | |
"\n", | |
"for n in fab(5): \n", | |
" print(n) " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1. Yield function is not a merely a function, it's generator\n", | |
"2. call fab(5) won't execute fab, instead, it return a interable\n", | |
"3. when calling the fab, it executes until yield function and return. Then, continues to execute" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"5\n", | |
"1\n", | |
"1\n", | |
"2\n" | |
] | |
} | |
], | |
"source": [ | |
"f = fab(5)\n", | |
"print(next(f))\n", | |
"print(next(f))\n", | |
"print(next(f))\n", | |
"print(next(f))\n", | |
"print(next(f))\n", | |
"\n", | |
"for i in fab(3):\n", | |
" print(i)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example read_file memory" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def read_file(fpath): \n", | |
" BLOCK_SIZE = 1024 \n", | |
" with open(fpath, 'rb') as f: \n", | |
" while True: \n", | |
" block = f.read(BLOCK_SIZE) \n", | |
" if block: \n", | |
" yield block \n", | |
" else: \n", | |
" return" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"reference :\n", | |
"1. https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/index.html\n", | |
"2. https://www.programiz.com/python-programming/generator" | |
] | |
} | |
], | |
"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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment