Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Generators"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 9,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "def gen1(x):\n",
  17. " print(\"Inside Gen\")\n",
  18. " i = 1\n",
  19. " while(i<=x):\n",
  20. " print(\"Before Yield\")\n",
  21. " yield i\n",
  22. " print(\"After Yield\")\n",
  23. " i += 1"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 10,
  29. "metadata": {},
  30. "outputs": [
  31. {
  32. "data": {
  33. "text/plain": [
  34. "<generator object gen1 at 0x10ce452a0>"
  35. ]
  36. },
  37. "execution_count": 10,
  38. "metadata": {},
  39. "output_type": "execute_result"
  40. }
  41. ],
  42. "source": [
  43. "gen1(10)"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": 11,
  49. "metadata": {},
  50. "outputs": [
  51. {
  52. "name": "stdout",
  53. "output_type": "stream",
  54. "text": [
  55. "Inside Gen\n",
  56. "Before Yield\n",
  57. "1\n",
  58. "After Yield\n",
  59. "Before Yield\n",
  60. "2\n",
  61. "After Yield\n",
  62. "Before Yield\n",
  63. "3\n",
  64. "After Yield\n"
  65. ]
  66. }
  67. ],
  68. "source": [
  69. "for n in gen1(3):\n",
  70. " print(n)"
  71. ]
  72. },
  73. {
  74. "cell_type": "markdown",
  75. "metadata": {},
  76. "source": [
  77. "## Yield"
  78. ]
  79. },
  80. {
  81. "cell_type": "code",
  82. "execution_count": 12,
  83. "metadata": {},
  84. "outputs": [],
  85. "source": [
  86. "def gen2():\n",
  87. " yield 1\n",
  88. " yield \"one\"\n",
  89. " yield True\n",
  90. " yield [1, \"one\", True]\n",
  91. " yield {1: True}"
  92. ]
  93. },
  94. {
  95. "cell_type": "code",
  96. "execution_count": 13,
  97. "metadata": {},
  98. "outputs": [
  99. {
  100. "name": "stdout",
  101. "output_type": "stream",
  102. "text": [
  103. "1\n",
  104. "one\n",
  105. "True\n",
  106. "[1, 'one', True]\n",
  107. "{1: True}\n"
  108. ]
  109. }
  110. ],
  111. "source": [
  112. "for x in gen2():\n",
  113. " print(x)"
  114. ]
  115. },
  116. {
  117. "cell_type": "markdown",
  118. "metadata": {},
  119. "source": [
  120. "# Decorators"
  121. ]
  122. },
  123. {
  124. "cell_type": "code",
  125. "execution_count": 14,
  126. "metadata": {},
  127. "outputs": [
  128. {
  129. "name": "stdout",
  130. "output_type": "stream",
  131. "text": [
  132. "5.0\n"
  133. ]
  134. }
  135. ],
  136. "source": [
  137. "def my_function(x):\n",
  138. " print(x/2)\n",
  139. "\n",
  140. "my_function(10)"
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "execution_count": 15,
  146. "metadata": {},
  147. "outputs": [],
  148. "source": [
  149. "def half(some_function):\n",
  150. " def wrapper_function(x):\n",
  151. " some_function(x/2)\n",
  152. " return wrapper_function"
  153. ]
  154. },
  155. {
  156. "cell_type": "code",
  157. "execution_count": 16,
  158. "metadata": {},
  159. "outputs": [
  160. {
  161. "name": "stdout",
  162. "output_type": "stream",
  163. "text": [
  164. "2.5\n"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "half(my_function)(10)"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": 18,
  175. "metadata": {},
  176. "outputs": [],
  177. "source": [
  178. "@half\n",
  179. "def my_function(x):\n",
  180. " print(x/2)"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": 19,
  186. "metadata": {},
  187. "outputs": [
  188. {
  189. "name": "stdout",
  190. "output_type": "stream",
  191. "text": [
  192. "2.5\n"
  193. ]
  194. }
  195. ],
  196. "source": [
  197. "my_function(10)"
  198. ]
  199. },
  200. {
  201. "cell_type": "code",
  202. "execution_count": null,
  203. "metadata": {},
  204. "outputs": [],
  205. "source": []
  206. }
  207. ],
  208. "metadata": {
  209. "kernelspec": {
  210. "display_name": "Python 3",
  211. "language": "python",
  212. "name": "python3"
  213. },
  214. "language_info": {
  215. "codemirror_mode": {
  216. "name": "ipython",
  217. "version": 3
  218. },
  219. "file_extension": ".py",
  220. "mimetype": "text/x-python",
  221. "name": "python",
  222. "nbconvert_exporter": "python",
  223. "pygments_lexer": "ipython3",
  224. "version": "3.7.0"
  225. }
  226. },
  227. "nbformat": 4,
  228. "nbformat_minor": 2
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement