Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Generators"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [],
- "source": [
- "def gen1(x):\n",
- " print(\"Inside Gen\")\n",
- " i = 1\n",
- " while(i<=x):\n",
- " print(\"Before Yield\")\n",
- " yield i\n",
- " print(\"After Yield\")\n",
- " i += 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<generator object gen1 at 0x10ce452a0>"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "gen1(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Inside Gen\n",
- "Before Yield\n",
- "1\n",
- "After Yield\n",
- "Before Yield\n",
- "2\n",
- "After Yield\n",
- "Before Yield\n",
- "3\n",
- "After Yield\n"
- ]
- }
- ],
- "source": [
- "for n in gen1(3):\n",
- " print(n)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Yield"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [],
- "source": [
- "def gen2():\n",
- " yield 1\n",
- " yield \"one\"\n",
- " yield True\n",
- " yield [1, \"one\", True]\n",
- " yield {1: True}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "one\n",
- "True\n",
- "[1, 'one', True]\n",
- "{1: True}\n"
- ]
- }
- ],
- "source": [
- "for x in gen2():\n",
- " print(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Decorators"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5.0\n"
- ]
- }
- ],
- "source": [
- "def my_function(x):\n",
- " print(x/2)\n",
- "\n",
- "my_function(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [],
- "source": [
- "def half(some_function):\n",
- " def wrapper_function(x):\n",
- " some_function(x/2)\n",
- " return wrapper_function"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2.5\n"
- ]
- }
- ],
- "source": [
- "half(my_function)(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "@half\n",
- "def my_function(x):\n",
- " print(x/2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2.5\n"
- ]
- }
- ],
- "source": [
- "my_function(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "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.7.0"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement