{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# \ud30c\uc774\uc36c \ub370\ucf54\ub808\uc774\ud130 \uc774\ud574\ud558\uae30\n", "\n", "\n", "- \uc774\ud574\ud558\ub824\uba74 functional programming \uac1c\ub150\uc744 \uc54c\uc544\uc57c \ud568\n", "- Python\uc758 \ud568\uc218\ub97c \uc815\uc758\ud558\ub294 \ubc29\ubc95\uacfc \ud638\ucd9c\ud558\ub294 \ubb38\ubc95\uc5d0 \uc775\uc219\ud574\uc838\uc57c \ud568. \ubc18\uba74\uc5d0 decorator\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc740 \uc27d\ub2e4.\n", "\n", "## \uc6d0\ubcf8\n", "\n", "- [\ud30c\uc774\uc36c \ub370\ucf54\ub808\uc774\ud130 \uc774\ud574\ud558\uae30](http://codeflow.co.kr/question/731/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%8D%B0%EC%BD%94%EB%A0%88%EC%9D%B4%ED%84%B0-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " return 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "1" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Scope" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_string = 'This is a global variable'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " print locals()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# print globals()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{}\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. variable resolution rules" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_string = 'This is a global variable'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " print a_string" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a global variable\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " a_string = 'test'\n", " print locals()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'a_string': 'test'}\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "a_string" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "'This is a global variable'" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uae00\ub85c\ubc8c \ubcc0\uc218\ub97c \uc811\uadfc\ud560 \uc218 \uc788\uace0 mutable \ud0c0\uc785\uc774\ub77c\uba74 \uc218\uc815\ub3c4 \uac00\ub2a5\ud558\uc9c0\ub9cc, (\uae30\ubcf8\uc801\uc73c\ub85c) \ub2e4\ub978 \uac12\uc73c\ub85c \ud560\ub2f9(assign) \ud560 \uc218\ub294 \uc5c6\ub2e4.\n", "- \uc0ac\uc2e4 \uae00\ub85c\ubc8c \ubcc0\uc218\ub97c '\uac00\ub9ac\ub294' \uc0c8\ub85c\uc6b4 \ub85c\uceec \ubcc0\uc218\ub97c \ub9cc\ub4e6\n", "- \uc6d0\ub798 a_string\uc740 \uae00\ub85c\ubc8c \ubcc0\uc218\uc9c0\ub9cc locals \ud588\ub294\ub370 a_string\uc774 \ucd9c\ub825\ub410\ub2e4\ub294\uac74 local \uc774\ub77c\ub294\ub73b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Variable lifetime" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " x = 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "print x" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- foo \ud568\uc218\ub97c \uc704\ud55c namespace\ub294 \ud568\uc218 \ud638\ucd9c\uc2dc\ub9c8\ub2e4 \uc0dd\uc131\ub418\uace0, \ud568\uc218\uac00 \uc885\ub8cc\ub420\ub54c\ub9c8\ub2e4 \uc0ac\ub77c\uc9c4\ub2e4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Function arguments and parameters\n", "\n", "- \ud30c\uc774\uc36c \ud568\uc218\uc5d0 \uc778\uc790\ub97c \uc804\ub2ec \uac00\ub2a5\n", "- parameter \uc774\ub984\ub4e4\uc740 \ud568\uc218\uc758 \ub85c\uceec \ubcc0\uc218\ub4e4\uc774 \ub428" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo(x):\n", " print locals()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "foo(1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'x': 1}\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud568\uc218\uc758 \ub9e4\uac1c\ubcc0\uc218 \ubaa9\ub85d(parameter)\uc640 \uadf8\uc5d0 \ub9de\ub294 \uc778\uc790(argument)\ub97c \ub118\uae30\ub294 \ubc29\ubc95\uc774 \uc5ec\ub7ff \uc874\uc7ac\n", "- \ud568\uc218 \ub9e4\uac1c\ubcc0\uc218\ub294 \uaf2d \uc804\ub2ec\ud574\uc57c \ud558\ub294, \uc704\uce58\uae30\ubc18 \ud615\ud0dc\uc774\uac70\ub098, \ud638\ucd9c\uc2dc \uc804\ub2ec\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub418\ub294, \uc774\ub984\uc788\ub294, \ub610\ub294 default \uac12\uc744 \uac00\uc9c0\ub294 \ud615\ud0dc" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo(x, y=0): # 1\n", " return x - y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "# \uc704\uce58\uc5d0 \ub530\ub978 \uc778\uc790 \ub118\uae40\n", "foo(3, 1) # 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "2" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "# x\uc758 \uc778\uc790\ub9cc \ub118\uae40. y\ub294 default\uac12\n", "foo(3) # 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "3" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "# \ucd5c\uc18c\ud55c x\uc758 \uc778\uc790\ub294 \ub118\uaca8\uc57c \ub418\ub294\ub370 \uc5c6\uc5b4\uc11c Error\n", "foo() # 4" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "foo() takes at least 1 argument (0 given)", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# \ucd5c\uc18c\ud55c x\uc758 \uc778\uc790\ub294 \ub118\uaca8\uc57c \ub418\ub294\ub370 \uc5c6\uc5b4\uc11c Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: foo() takes at least 1 argument (0 given)" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "# keyword \ud615\ud0dc\ub85c \uc778\uc790 \ub118\uae40.\n", "foo(y=1, x=3) # 5" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "2" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "# keyword \ub4a4\uc5d0 keyword \uc5c6\ub294\uac78 \ub118\uae30\uba74 \uc548\ub428.\n", "# \uc989, \ub0b4 \uc758\ub3c4\ub294 y=1, x=2 \ub77c\uace0 \uc0dd\uac01\ud558\uace0 \ub118\uacbc\uc9c0\ub9cc\n", "# \uc778\ud130\ud504\ub9ac\ud130\ub294 \uadf8\ub807\uac8c \uc0dd\uac01\ud558\uc9c0 \uc54a\ub294\ub2e4\ub294 \uc774\uc57c\uae30\n", "foo(y=1, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "non-keyword arg after keyword arg (, line 4)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m foo(y=1, 2)\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-keyword arg after keyword arg\n" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Nested functions\n", "\n", "- \ud30c\uc774\uc36c\uc5d0\uc11c\ub294 \uc911\ucca9\ub41c \ud568\uc218\ub97c \uc0dd\uc131 \uac00\ub2a5\n", "- \uc989, \ud568\uc218 \uc548\uc5d0 \ud568\uc218\ub97c \uc120\uc5b8\ud560 \uc218 \uc788\uc73c\uba70, scope\uc640 lifetime \ubc95\uce59\ub4e4\uc774 \ubaa8\ub450 \uc801\uc6a9\ub41c\ub2e4\ub294 \ub73b\n", "- \uc5ec\uae30\uc11c\ubd80\ud130 \uc5b4\ub824\uc6cc\uc9c4\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer():\n", " x = 1\n", " def inner():\n", " print x # 1\n", " inner() # 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "outer()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \\#1: x\ub77c\ub294 \uc774\ub984\uc758 \ub85c\uceec \ubcc0\uc218\ub97c \ucc3e\ub294\ub2e4. \ucc3e\ub294\ub370 \uc2e4\ud328\ud558\uace0\uc120 \uc774 scope\ub97c \uac10\uc2f8\ub294 scope, \uc5ec\uae30\uc36c \ub610 \ub2e4\ub978 \ud568\uc218\uc758 scope\ub97c \ucc3e\uac8c \ub41c\ub2e4.\n", "- x\ub294 outer \ud568\uc218\uc758 \ub85c\uceec \ubcc0\uc218\uc774\ub2e4. \ud558\uc9c0\ub9cc inner \ud568\uc218\ub294, \uc790\uc2e0\uc744 \uac10\uc2f8\uace0 \uc788\ub294 scope\uc758 \ubcc0\uc218\ub4e4\uc5d0 \ub300\ud55c (\uc801\uc5b4\ub3c4 read/modify) \uc811\uadfc\uc744 \ud560 \uc218 \uc788\ub2e4.\n", "- \\#2: \uc5d0\uc11c inner \ud568\uc218\ub97c \ud638\ucd9c\ud55c\ub2e4. inner\ub3c4 \ubcc0\uc218\uc758 \uc774\ub984\uc77c \ubfd0\uc774\uace0, \ubcc0\uc218\ub97c \ucc3e\ub294 \ubc95\uce59\uc774 \ub611\uac19\uc774 \uc801\uc6a9\n", "- outer scope\uc744 \uba3c\uc800 \ucc3e\uc544\ubcf4\uace0, inner\ub780 \ubcc0\uc218\ub97c \ubc1c\uacac" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer():\n", " x = 1\n", " def inner():\n", " print x # 1\n", " print locals()\n", " print inner\n", " inner() # 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "# locals\uc5d0 x\uc640 inner \uac00 \uac19\uc774 \uc788\ub124. inner\ub3c4 \ud568\uc218\ub97c \ub2f4\uace0 \uc788\ub294 \ubcc0\uc218\ub85c \uc778\uc2dd\n", "outer()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'x': 1, 'inner': }\n", "\n", "1\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Functions are first class objects in Python\n", "\n", "- \ud568\uc218\ub294 \ub2e4\ub978 \ubaa8\ub4e0 \uac83\ucc98\ub7fc \uac1d\uccb4\ub77c\ub294 \uac83\uc744 \uc758\ubbf8\n", "- \ubcc0\uc218\ub97c \ud3ec\ud568\ud55c \ud568\uc218\ub4e4\ub3c4 \ud2b9\ubcc4\ud55c \uac83\uc774 \uc544\ub2c8\ub2e4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# all objects in Python inherit from a common baseclass\n", "issubclass(int, object)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "True" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " pass" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "foo.__class__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "function" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "issubclass(foo.__class__, object)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "True" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc544\ub9c8\ub3c4, \ud568\uc218\uac00 \uc18d\uc131\uc744 \uac16\ub294\ub2e4\uace0 \uc0dd\uac01\ud574 \ubcf8\uc801\uc740 \uc5c6\uc744 \uac83\n", "- \ud558\uc9c0\ub9cc \ud30c\uc774\uc36c\uc5d0\uc11c \ud568\uc218\ub294 \ub2e4\ub978 \ubaa8\ub4e0 \uac83\ub4e4\ucc98\ub7fc \uac1d\uccb4\uc774\ub2e4. (\uc774 \ub9d0\uc774 \uc774\uc0c1\ud558\uac8c \ub4e4\ub9b0\ub2e4\uba74, \ud30c\uc774\uc36c\uc5d0\uc120 \ud074\ub798\uc2a4\ub294 \ub2e4\ub978 \ubaa8\ub4e0 \uac83\ub4e4\ucc98\ub7fc \uac1d\uccb4\uc774\ub2e4! \ub77c\ub294 \ub9d0\uc740 \uc5b4\ub5a4\uac00)\n", "- \ud568\uc218\ub294 \ud30c\uc774\uc36c\uc5d0\uc11c \ub2e4\ub978 \ubaa8\ub4e0 \uac12\ub4e4\ucc98\ub7fc \ud558\ub098\uc758 \uac12\uc77c \ubfd0\n", "- \uc774 \ub9d0\uc740, \ud568\uc218\ub97c \ub2e4\ub978 \ud568\uc218\uc5d0\uac8c \uc778\uc790\ub85c \ub118\uae38 \uc218 \uc788\uace0, \ud568\uc218\uc5d0\uc11c \ud568\uc218\ub97c \ub9ac\ud134\ud560 \uc218 \uc788\ub2e4!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(x, y):\n", " return x + y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "def sub(x, y):\n", " return x - y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "# \uc544 \uc5ec\uae30\uc5d0\uc11c apply \ud568\uc218\uac00 \ub098\uc624\ub124\n", "# \uc694\uc998 \ud30c\uc774\uc36c \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \ud65c\uc6a9\ud55c \ub370\uc774\ud130 \ubd84\uc11d \ud558\ub294\ub370 apply \ud568\uc218\uac00 \uc790\uc8fc \ub4f1\uc7a5\ud558\ub358\ub370..\n", "# \ub0b4\uac00 \uc124\ub801 \ubc30\uc6cc\uc11c \uadf8\ub7ac\uad6c\ub098..\u3160\u3160\n", "def apply(func, x, y): # 1\n", " return func(x, y) # 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "# \ud568\uc218 \uc774\ub984\uc740 \ub2e4\ub978 \ubcc0\uc218\ub4e4\ucc98\ub7fc \ubcc0\uc218\uc758 \uc774\ub984\uc77c \ubfd0\n", "apply(add, 2, 1) # 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "3" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "apply(sub, 2, 1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "1" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud30c\uc774\uc36c sorted \ub0b4\uc7a5 \ud568\uc218\uc758 key \ud30c\ub77c\ubbf8\ud130 \uc704\uce58\uc5d0, \uc9c1\uc811 \uc791\uc131\ud55c \ud568\uc218\ub97c \uc804\ub2ec\ud558\uace4 \ud55c\ub2e4. \uc544\ud558!! \uc0dd\uac01\ub09c\ub2e4. sorted \uc5d0 \ub0b4\uac00 \uc9c1\uc811 \uc791\uc131\ud55c \ud568\uc218\ub97c \ub118\uae34 \uc801\uc774 \uc788\uc9c0.\n", "- \ud568\uc218\ub97c \ub9ac\ud134\ud558\ub294 \uac83\ub3c4 \uc0b4\ud3b4\ubcf4\uc790" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer():\n", " def inner():\n", " print 'Inside inner'\n", " return inner # 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "foo = outer() # 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "foo # doctest:+ELLIPSIS" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Inside inner\n" ] } ], "prompt_number": 40 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc774\uc0c1\ud574 \ubcf4\uc778\ub2e4!\n", "- \\#1\uc5d0\uc11c inner \ubcc0\uc218\ub294 \ud568\uc218\uc758 \uc774\ub984\uc774\ub2e4. \ud2b9\ubcc4\ud55c \ubb38\ubc95\uc744 \uc0ac\uc6a9\ud558\uace0 \uc788\uc9c0\ub294 \uc54a\ub2e4.\n", "- outer \ud568\uc218\ub294 inner \ud568\uc218\ub97c \ub9ac\ud134\ud558\uace0 \uc788\ub2e4. \ubcc0\uc218\uc758 lifetime\uc5d0 \ub300\ud55c \uc124\uba85\uc774 \uae30\uc5b5\ub098\ub294\uac00? inner \ud568\uc218\ub294 outer \uac00 \ud638\ucd9c\ub420 \ub54c\ub9c8\ub2e4 \ub9e4\ubc88 \uc0c8\ub86d\uac8c \ub2e4\uc2dc \uc815\uc758\ub41c\ub2e4. \ud558\uc9c0\ub9cc inner \ud568\uc218\uac00 \ub9ac\ud134\ub418\uc9c0 \uc54a\uc558\ub2e4\uba74, scoper \ubc16\uc73c\ub85c \ubc97\uc5b4\ub0ac\uc744 \ub54c \uc0ac\ub77c\uc838 \ubc84\ub838\uc744 \uac83\uc774\ub2e4.\n", "- \uadf8\ub807\uc9c0! return\uc744 \uc548\ud558\uba74 inner \ubcc0\uc218\ub294 \uc0ac\ub77c\uc84c\uaca0\uc9c0. namespace \ubc94\uc704\ub97c \ub118\uc5b4\uc11c\ub294 \uac70\ub2c8\uae4c\n", "- \\#2\uc5d0\uc11c, \ub9ac\ud134\ub41c \uac12, \uc989 inner \ud568\uc218\ub97c \ubc1b\uc544\uc11c foo\ub77c\ub294 \uc0c8\ub85c\uc6b4 \ubcc0\uc218\uc5d0 \uc800\uc7a5\n", "- foo\ub97c evaluate \ud558\uba74 inner \ud568\uc218\ub77c\ub294 \uac83\uc744 \ud655\uc778\ud560 \uc218 \uc788\uace0, call operator(\uad04\ud638)\ub97c \uc0ac\uc6a9\ud558\uc5ec \ud638\ucd9c\ud560 \uc218 \uc788\ub2e4. \uc57d\uac04 \uc774\uc0c1\ud558\uae34 \ud558\uc9c0\ub9cc, \uc774\ud574\ud558\uae30\uac00 \uc5b4\ub835\uc9c4 \uc54a\uc744 \uac83" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. Closures" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer():\n", " x = 1\n", " def inner():\n", " print x # 1\n", " print locals()\n", " return inner" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "foo = outer()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'x': 1, 'inner': }\n" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "# inner\ub9cc \ub9ac\ud134\ub418\uc5c8\uae30 \ub54c\ubb38\uc5d0 print locals\ub294 \uc2e4\ud589 \uc548\ub428\n", "# x\ub294 outer\uc758 \ub85c\uceec\ubcc0\uc218\uc774\uace0 inner \ud568\uc218\ub9cc \ub9ac\ud134\ub410\ub294\ub370 \uc5b4\ub5bb\uac8c x\uac00 \uc0b4\uc544\ub0a8\uc744 \uc218\uac00 \uc788\uc9c0?\n", "# function closures \ub77c\ub294 \uae30\ub2a5 \ub54c\ubb38\uc5d0 \uc5d0\ub7ec \ubc1c\uc0dd \uc548\ud568\n", "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "foo.func_closure # doctest: + ELLIPSIS" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "(,)" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- outer\ub97c \ud638\ucd9c\ud558\uc5ec \uc5bb\uc740 \uac12\uc774 inner \ud568\uc218\ub77c\ub294 \uac83\uc744 \uc548\ub2e4\n", "- \uc774 \ud568\uc218\uac00 foo\uc5d0 \uc800\uc7a5\ub418\uc5b4 \uc788\uace0 foo()\ub85c \ud638\ucd9c\ud560 \uc218 \uc788\ub2e4.\n", "- \ud558\uc9c0\ub9cc \uc5d0\ub7ec\uc5c6\uc774 \uc2e4\ud589\ub420\uae4c? \n", "- scope rule\uc744 \uba3c\uc800 \uc0b4\ud3b4\ubcf4\uc790.\n", "- \ubaa8\ub4e0 \uac83\uc740 python scope rule\uc744 \ub530\ub978\ub2e4.\n", "- x\ub294 outer \ud568\uc218 \ub0b4\uc758 \ub85c\uceec \ubcc0\uc218\uc774\ub2e4. \n", "\n", "### function closures\n", "\n", "- global\uc774 \uc544\ub2cc \uacf3\uc5d0\uc11c \uc815\uc758\ub41c \ub0b4\ubd80 \ud568\uc218\uac00, **\ud568\uc218\uac00 \uc815\uc758\ub418\ub294 \uc2dc\uc810\uc5d0** \uc790\uc2e0\uc744 \uac10\uc2f8\uace0 \uc788\ub294 namespace\uac00 \uc5b4\ub5bb\uac8c \uc0dd\uacbc\ub294\uc9c0 \uae30\uc5b5\ud55c\ub2e4\ub294 \ub73b\n", "- \ub0b4\ubd80 \ud568\uc218(\uc989 inner)\uc758 func_closure \ub77c\ub294 \uc18d\uc131\uc744 \ubcf4\uba74, \ud568\uc218\ub97c \uac10\uc2f8\uace0 \uc788\ub294 scope\uc758 \ubcc0\uc218\ub4e4\uc744 \ubcfc \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer(x):\n", " def inner():\n", " print x # 1\n", " return inner" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "print1 = outer(1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "print2 = outer(2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "print1()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "print2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc774 \uc608\uc81c\ub97c \ubcf4\uba74 closure \\- \uc790\uc2e0\uc744 \uac10\uc2f8\uace0 \uc788\ub294 scope\ub97c \uae30\uc5b5\ud558\ub294 \ud568\uc218 \\-\ub97c \uc774\uc6a9\ud574, \uc778\uc790\ub97c hard-coding\ud55c \ud568\uc218\ub4e4\uc744 \ub9cc\ub4e4 \uc218 \uc788\ub2e4\ub294 \uac83\uc744 \uc54c \uc218 \uc788\ub2e4.\n", "- \uc22b\uc790 1, 2\ub97c inner \ud568\uc218\uc5d0\uac8c \uc804\ub2ec\ud558\ub294 \uac83\uc774 \uc544\ub2c8\ub77c, 1, 2\ub97c \uae30\uc5b5\ud558\uace0 \uc788\ub294 \ud568\uc218\ub97c \uba87 \uac1c \ub9cc\ub4e0 \uac83\uc774\ub2e4.\n", "- \uc774\uac83\uc740 \uad49\uc7a5\ud788 \uac15\ub825\ud55c \uae30\uc220\uc774\ub2e4. - \uac1d\uccb4 \uc9c0\ud5a5 \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \ub5a0\uc62c\ub9b4 \uc218\ub3c4 \uc788\ub2e4.\n", "- outer\ub294 \uba64\ubc84 \ubcc0\uc218 x\ub97c \uac00\uc9c4 inner \ud568\uc218\uc758 \uc0dd\uc131\uc790\ub77c\uace0 \ubcfc \uc218 \uc788\ub2e4. \uc774\uc6a9\ud560 \uc218 \uc788\ub294 \uc601\uc5ed\uc740 \ub9ce\ub2e4.\n", "- sorted \ud568\uc218\uc758 key \ud30c\ub77c\ubbf8\ud130\ub97c \ubcf8 \uc801\uc774 \uc788\ub2e4\uba74, \uc544\ub9c8\ub3c4 \ub9ac\uc2a4\ud2b8\uc758 \uccab\ubc88\uc9f8 \uc6d0\uc18c\uac00 \uc544\ub2cc \ub450\ubc88\uc9f8 \uc6d0\uc18c\ub85c \ub9ac\uc2a4\ud2b8\uc758 \ub9ac\uc2a4\ud2b8\ub97c \uc815\ub82c\ud558\ub294 lambda \ud568\uc218\ub97c \uc791\uc131\ud574 \ubcf8 \uc801\uc774 \uc788\uc744 \uac83\uc774\ub2e4. \uc774\uc81c \uc0ac\uc6a9\ud560 index\ub97c \ubc1b\uc544\uc11c key parameter\uc5d0 \uc0ac\uc6a9\ud560 \ud568\uc218\ub97c \ub9ac\ud134\ud558\ub294 itemgetter \ud568\uc218\ub97c \ub9cc\ub4e4 \uc218\ub3c4 \uc788\ub2e4.\n", "\n", "- \ud558\uc9c0\ub9cc \uc9c0\ub8e8\ud55c closure\ub294 \uc774\uc81c \uadf8\ub9cc \ubcf4\ub3c4\ub85d \ud558\uc790! decorator\ub85c \ub118\uc5b4\uac00\uc790!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9. Decorators!\n", "\n", "- \ub370\ucf54\ub808\uc774\ud130\ub294 \ud568\uc218\ub97c \uc778\uc790\ub85c \ubc1b\uc544\uc11c \ud568\uc218\ub97c \ub9ac\ud134\ud558\ub294 callable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def outer(some_func):\n", " def inner():\n", " print 'before some_func'\n", " ret = some_func() # 1\n", " return ret + 1\n", " return inner" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " return 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "decorated = outer(foo) # 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "decorated()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "before some_func\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "2" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "foo = outer(foo)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "foo" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- coodinate(\uc88c\ud45c) \uac1d\uccb4\ub97c \uc0ac\uc6a9\ud558\ub294 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc0dd\uac01\ud574 \ubcf4\uc790\n", "- \uc544\ub9c8\ub3c4 x\uc640 y\uc88c\ud45c \uc30d\uc744 \uac00\uc9c8 \uac83\n", "- \uc548\ud0c0\uae5d\uac8c\ub3c4 \uc774 \uc88c\ud45c \uac1d\uccb4\ub4e4\uc740 \uc218\ud559\uc801 \uc5f0\uc0b0\ub4e4\uc744 \uc9c0\uc6d0\ud558\uc9c0 \uc54a\uace0, \uc6b0\ub9ac\ub294 \ub77c\uc774\ube0c\ub7ec\ub9ac \uc18c\uc2a4\ub97c \ubcc0\uacbd\ud560 \uc218\ub3c4 \uc5c6\ub2e4\n", "- \ub450 coordinate \uac1d\uccb4\ub97c \ubc1b\uc544\uc11c add\uc640 sub \ud568\uc218\ub97c \ub9cc\ub4e4\uac83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Coordinate(object):\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", " def __repr__(self):\n", " return 'Coord: ' + str(self.__dict__)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "def add(a, b):\n", " return Coordinate(a.x + b.x, a.y + b.y)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "def sub(a, b):\n", " return Coordinate(a.x - b.x, a.y - b.y)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "one = Coordinate(100, 200)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "two = Coordinate(300, 200)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "add(one, two)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "Coord: {'y': 400, 'x': 400}" ] } ], "prompt_number": 61 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uac12\uc758 \uc720\ud6a8\uc131\uc744 \uac80\uc0ac\ud574\uc57c \ud55c\ub2e4\uba74 \uc5b4\ub5bb\uac8c \ud560\uae4c?\n", "- \uc544\ub9c8\ub3c4 \ud50c\ub7ec\uc2a4 \uc88c\ud45c\ub4e4\ub9cc \ub354\ud558\uac70\ub098 \ube7c\uac8c \ud560 \uc218\ub3c4 \uc788\uace0, \uacb0\uacfc\uac12\uc774 \ud50c\ub7ec\uc2a4\ub85c\ub9cc \ub098\uc640\uc57c \ub418\ub3c4\ub85d \uc870\uac74\uc744 \uc904 \uc218\ub3c4 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "one = Coordinate(100, 200)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "two = Coordinate(300, 200)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "three = Coordinate(-100, -100)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "sub(one, two)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "Coord: {'y': 0, 'x': -200}" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "add(one, three)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 66, "text": [ "Coord: {'y': 100, 'x': 0}" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "def wrapper(func):\n", " def checker(a, b): # 1\n", " # \uc785\ub825\uac12\uc774 0\ubcf4\ub2e4 \uc791\ub2e4\uba74 \ubaa8\ub450 0\uc73c\ub85c \uc124\uc815\n", " if a.x < 0 or a.y < 0:\n", " a = Coordinate(a.x if a.x > 0 else 0, a.y if a.y > 0 else 0)\n", " if b.x < 0 or b.y < 0:\n", " b = Coordinate(b.x if b.x > 0 else 0, b.y if b.y > 0 else 0)\n", " ret = func(a, b)\n", " # return \uac12\uc5d0\uc11c x\ub098 y\uac00 \ud558\ub098\ub77c\ub3c4 0\ubcf4\ub2e4 \uc791\ub2e4\uba74\n", " if ret.x < 0 or ret.y < 0:\n", " ret = Coordinate(ret.x if ret.x > 0 else 0, \n", " ret.y if ret.y > 0 else 0)\n", " return ret\n", " return checker" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "add = wrapper(add)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "# checker \ud568\uc218\uaca0\uc9c0\n", "add" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 69, "text": [ "" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "sub = wrapper(sub)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "# checker \ud568\uc218\uac00 a, b\ub97c \uae30\uc5b5\ud558\ub294 \uac83\uc740 closures \ub54c\ubb38\n", "sub(one, two)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 71, "text": [ "Coord: {'y': 0, 'x': 0}" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "add(one, three)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "Coord: {'y': 200, 'x': 100}" ] } ], "prompt_number": 72 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud568\uc218 \uc778\uc790\uc640 \uacb0\uacfc\uac12\uc744 \ud655\uc778\ud574\uc11c \ub9c8\uc774\ub108\uc2a4 x, y\uac12\uc744 0\uc73c\ub85c \ubcc0\uacbd\n", "- \ub370\ucf54\ub808\uc774\ud130\uc5d0 \ubc14\uc6b4\ub4dc \uccb4\ud0b9 \uae30\ub2a5\uc744 \uad6c\ud604\ud558\uace0 \ubaa8\ub4e0 \ud568\uc218\uc5d0 \ub370\ucf54\ub808\uc774\ud130\ub97c \uc801\uc6a9\ud558\ub294 \uac83 \ub9d0\uc774\ub2e4.\n", "- \ub2e4\ub978 \ubc29\ubc95\uc73c\ub85c\ub294, \ud568\uc218\uc778\uc790\ub97c \ud655\uc778\ud558\ub294 \ud568\uc218\ub97c \ubd80\ub974\uace0, \ub9ac\ud134\ud558\uae30 \uc804\uc5d0 \ud655\uc778\ud558\ub294 \ud568\uc218\ub97c \ubd80\ub97c \uc218 \uc788\ub2e4.\n", "- decorator\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uc911\ubcf5\uc774 \ub35c\ud558\ub2e4\ub294 \uac83\uc740 \ud655\uc2e4\ud558\ub2e4.\n", "- \uadf8\ub9ac\uace0 \ub77c\uc774\ube0c\ub7ec\ub9ac \ucf54\ub4dc\uac00 \uc544\ub2cc \uc6b0\ub9ac\uc758 \ucf54\ub4dc\uc5d0 \ub370\ucf54\ub808\uc774\ud130\ub97c \uc801\uc6a9\ud55c\ub2e4\uba74, \ub370\ucf54\ub808\uc774\ud130\ub97c \uc0ac\uc6a9\ud55c\ub2e4\ub294 \uac83\uc744 \ub354 \uba85\ud655\ud788 \ud45c\ud604\ud560 \uc218 \uc788\ub2e4.\n", "- \uc778\uc790\uac12 \uac80\uc0ac\ud560 \ub54c\ub3c4 \uc720\uc6a9\ud558\uac8c \uc0ac\uc6a9\ud560\ub4ef" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 10. The @ symbol applies a decorator to a function\n", "\n", "- @decorator_name\ub97c \ud568\uc218 \uc815\uc758 \uc55e\uc5d0 \ubd99\uc5ec\uc11c decorator\ub97c \uc801\uc6a9\ud560 \uc218 \uc788\uac8c \ud568" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decorator\ub85c \ubcc0\ud615\ud55c \ud568\uc218\ub97c \ubcc0\uc218\uc5d0 \uc800\uc7a5\ud558\uc5ec \uc0ac\uc6a9.. \uadc0\ucc2e\n", "add = wrapper(add)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "@wrapper\n", "def add(a, b):\n", " return Coordinate(a.x + b.x, a.y + b.y)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 74 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- add\ub97c wrapper \ud568\uc218\uc758 \uacb0\uacfc \uac12\uc73c\ub85c \uc124\uc815\ud55c\ub2e4\ub294 \uc810\uc740 \ub611\uac19\ub2e4.\n", "- \ub2e8\uc9c0 \uc774 \ub0b4\uc6a9\uc774 \ubd84\uba85\ud788 \ub4dc\ub7ec\ub098\ub3c4\ub85d \ud30c\uc774\uc36c\uc5d0\uc11c \ubb38\ubc95\uc744 \uc81c\uacf5\ud574 \uc8fc\ub294 \uac83\n", "- \ub2e4\uc2dc \ub9d0\ud558\uc9c0\ub9cc - \ub370\ucf54\ub808\uc774\ud130\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc740 \uc27d\ub2e4.\n", "- staticmethod\ub098 classmethod \uac19\uc740 \uc720\uc6a9\ud55c \ub370\ucf54\ub808\uc774\ud130\ub97c \uc791\uc131\ud558\ub294 \uac83\uc740 \uc5b4\ub824\uc6b8\uc9c0 \ubaa8\ub974\uc9c0\ub9cc, \ub370\ucf54\ub808\uc774\ud130\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc740 \ub2e8\uc21c\ud788 @decorator_name \uc774\ub77c\uace0 \ud568\uc218 \uc55e\uc5d0 \ubd99\uc5ec\uc8fc\uae30\ub9cc \ud558\uba74 \ub41c\ub2e4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 11. args and *kwargs\n", "\n", "- decorator\ub97c \uc791\uc131\ud558\uc600\uc9c0\ub9cc, \ud2b9\ubcc4\ud55c \ud568\uc218\uc5d0\uac8c\ub9cc \uc801\uc6a9\ub420 \uc218 \uc788\ub294 \uac83\n", "- \ub450\uac1c\uc758 \uc778\uc790\ub97c \ubc1b\ub294 \ud568\uc218 \ub9d0\uc774\ub2e4.\n", "- \uc6b0\ub9ac\uc758 \ub0b4\ubd80 \ud568\uc218 checker\ub294 2\uac1c\uc758 \uc778\uc790\ub97c \ubc1b\uc544 closure\uc5d0\uc11c \uc5bb\uc740 \ud568\uc218\uc5d0\uac8c \uc778\uc790\ub85c \uc804\ub2ec\n", "- \ub9cc\uc57d, \uc5b4\ub5a4 \ud568\uc218\uc5d0\ub3c4 \uc801\uc6a9\ud560 \uc218 \uc788\ub294 decorator \ub97c \ub9cc\ub4e4\ub824\uba74 \uc5b4\ub5bb\uac8c \ud560\uae4c?\n", "- decorate \ub420 \ud568\uc218\ub97c \uc804\ud600 \ubcc0\uacbd\ud558\uc9c0 \uc54a\uace0, \ud568\uc218\ub4e4\uc774 \ubd88\ub9b4\ub54c\ub9c8\ub2e4 \uce74\uc6b4\ud130\ub97c \uc99d\uac00\uc2dc\ud0a4\ub294 \ub370\ucf54\ub808\uc774\ud130\ub97c \ub9cc\ub4e4\uc5b4 \ubcf4\uc790.\n", "- \uc774 decorator\ub294, \ubcc0\uacbd\ud560 \ud568\uc218\uc758 \uc778\uc790 \ubaa9\ub85d\uc774 \ubb34\uc5c7\uc774\ub4e0\uc9c0 \uc0c1\uad00\uc5c6\uc774, \uc790\uc2e0\uc774 \ubc1b\uc740 \uc778\uc790\ub4e4\uc744 \uc804\ub2ec\ud560 \uc218 \uc788\uc5b4\uc57c\ud55c\ub2e4.\n", "- \\*\ub85c \uac00\ub2a5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def one(*args):\n", " print args # 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "one()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "()\n" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "one(1, 2, 3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1, 2, 3)\n" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "def two(x, y, *args): # 2\n", " print x, y, args" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "two('a', 'b', 'c')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a b ('c',)\n" ] } ], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "two('a', 'b', 'c', 'd')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a b ('c', 'd')\n" ] } ], "prompt_number": 80 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uccab\ubc88\uc9f8 \ud568\uc218 one\uc740 \uc704\uce58\uae30\ubc18 \uc778\uc790\ub4e4\uc744 \ubc1b\uc544\uc11c \ucd9c\ub825\n", "- arg \ub77c\ub294 \ubcc0\uc218\ub97c \uc0ac\uc6a9. \\*args\ub294 \ud568\uc218 \uc815\uc758\uc5d0\uc11c\ub9cc \uc0ac\uc6a9\ub418\uace0 \uc704\uce58\uae30\ubc18 \uc778\uc790\ub4e4\uc774 args \ubcc0\uc218\uc5d0 \uc800\uc7a5\ub418\uc5b4\uc57c \ud55c\ub2e4\ub294 \ub73b\n", "- \\#2\uc5d0\uc11c \ubcf4\ub294 \uac83\ucc98\ub7fc \uba87 \uac1c\uc758 \ubcc0\uc218\ub97c \ubc1b\uc740 \ud6c4, \uadf8 \uc678\uc758 \ubcc0\uc218\ub4e4\uc744 args\ub85c \ubc1b\ub294 \uac83\ub3c4 \uac00\ub2a5\ud558\ub2e4.\n", "\n", "\n", "- \\* \uc624\ud37c\ub808\uc774\ud130\ub294 \ud568\uc218\ub97c \ud638\ucd9c\ud560 \ub54c \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\ub2e4. \uc774 \ub54c\uc5d0\ub3c4 \ube44\uc2b7\ud55c \ub73b\uc73c\ub85c \uc4f0\uc778\ub2e4. \ud568\uc218\ub97c **\ud638\ucd9c\ud560\ub54c** \\#\uac00 \uc55e\uc5d0 \ubd99\uc740 \ubcc0\uc218\ub294, \uc774 \ubcc0\uc218\uc758 \ub0b4\uc6a9\uc774 \uc704\uce58\uae30\ubc18 \uc778\uc790\ub85c \uc0ac\uc6a9\ub418\uc5b4\uc57c \ud55c\ub2e4\ub294 \ub73b" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(x, y):\n", " return x + y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "lst = [1, 2]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "add(lst[0], lst[1]) # 1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 83, "text": [ "3" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "add(*lst)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "3" ] } ], "prompt_number": 84 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \\*\\*\ub3c4 \ube44\uc2b7\ud558\ub2e4. \\* \uc774 iterable\uacfc \uc704\uce58\uae30\ubc18 \uc778\uc790\ub4e4\uc5d0 \uad00\ud55c \uac83\uc774\ub77c\uba74, \\*\\*\uc740 \ub515\uc154\ub108\ub9ac\uc640 \uc774\ub984\uc788\ub294 \uc778\uc790\ub4e4\uc5d0 \uad00\ud55c \uac83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo(**kwargs):\n", " print kwargs" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "foo()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{}\n" ] } ], "prompt_number": 86 }, { "cell_type": "code", "collapsed": false, "input": [ "foo(x=1, y=2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 2, 'x': 1}\n" ] } ], "prompt_number": 87 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud568\uc218\ub97c \uc815\uc758\ud560 \ub54c, \\*\\*kwargs \ub97c \uc0ac\uc6a9\ud558\uc5ec \uc0ac\uc6a9\ub418\uc9c0 \uc54a\uc740 \ubaa8\ub4e0 \uc778\uc790\ub4e4\uc774 kwargs \ub77c\ub294 \ub515\uc154\ub108\ub9ac\uc5d0\uac8c \uc800\uc7a5\ub420\uac8c \ud560 \uc218 \uc788\uc74c\n", "- args\ub098 kwargs\ub77c\ub294 \uc774\ub984\uc774 \uc911\uc694\ud55c \uac83\uc740 \uc544\ub2c8\uace0, \uc790\uc8fc \uc4f0\uc774\ub294 \ub2e8\uc5b4\uc77c \ubfd0.\n", "- \\*\ub97c \ud568\uc218\ub97c \uc815\uc758\ud560 \ub54c\uc640, \ud568\uc218\ub97c \ud638\ucd9c\ud560 \ub54c \uc0ac\uc6a9\ud558\ub4ef\uc774 \\*\\*\ub3c4 \ub9c8\ucc2c\uac00\uc9c0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dct = {'x': 1, 'y':2}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 88 }, { "cell_type": "code", "collapsed": false, "input": [ "def bar(x, y):\n", " return x + y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "bar(**dct)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "3" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "bar(*lst)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 91, "text": [ "3" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "dct2 = {'a':2, 'b':3}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 92 }, { "cell_type": "code", "collapsed": false, "input": [ "# bar \ud568\uc218\uc5d0 'a'\ub77c\ub294 keyword \uc778\uc790\uac00 \uc5c6\uc74c\n", "bar(**dct2)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "bar() got an unexpected keyword argument 'a'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# bar \ud568\uc218\uc5d0 'a'\ub77c\ub294 keyword \uc778\uc790\uac00 \uc5c6\uc74c\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mdct2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: bar() got an unexpected keyword argument 'a'" ] } ], "prompt_number": 93 }, { "cell_type": "code", "collapsed": false, "input": [ "bar(x=2, y=3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 94, "text": [ "5" ] } ], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "# \uc774\uac70\ub791 \uac19\uc740 \uac70\ub2e4.\n", "# \ub2f9\uc5f0\ud788 bar \ud568\uc218\uc5d0\ub294 a keyword \uc778\uc790\uac00 \uc5c6\uc74c.\n", "bar(a=2, b=3)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "bar() got an unexpected keyword argument 'a'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# \uc774\uac70\ub791 \uac19\uc740 \uac70\ub2e4.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# \ub2f9\uc5f0\ud788 bar \ud568\uc218\uc5d0\ub294 a keyword \uc778\uc790\uac00 \uc5c6\uc74c.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mbar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: bar() got an unexpected keyword argument 'a'" ] } ], "prompt_number": 95 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12. More generic decorators\n", "\n", "- \ud568\uc218\uc758 \uc778\uc790\ub97c 'log' \ud558\ub294 \ub370\ucf54\ub808\uc774\ud130\ub97c \ub9cc\ub4e4 \uc218 \uc788\uc74c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def logger(func):\n", " def inner(*args, **kwargs): #1\n", " print 'Arguments were: %s, %s' % (args, kwargs)\n", " return func(*args, **kwargs) #2\n", " return inner" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 96 }, { "cell_type": "code", "collapsed": false, "input": [ "@logger\n", "def foo1(x, y=1):\n", " return x * y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 97 }, { "cell_type": "code", "collapsed": false, "input": [ "@logger\n", "def foo2():\n", " return 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "foo1(5, 4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Arguments were: (5, 4), {}\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 99, "text": [ "20" ] } ], "prompt_number": 99 }, { "cell_type": "code", "collapsed": false, "input": [ "foo1(1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Arguments were: (1,), {}\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 100, "text": [ "1" ] } ], "prompt_number": 100 }, { "cell_type": "code", "collapsed": false, "input": [ "foo2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Arguments were: (), {}\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "2" ] } ], "prompt_number": 101 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \ud55c\uae00 \ubc84\uc804 decorator \uc124\uba85\n", "\n", "- [Python \ub370\ucf54\ub808\uc774\ud130](http://soooprmx.com/wp/archives/3999)\n", "- [Python \ub370\ucf54\ub808\uc774\ud130\uc5d0 \ub300\ud55c \uace0\ucc30](http://mcchae.egloos.com/11030528): \uc774 \ubd84 \uae00\uc774 \uc815\ub9d0 \uc88b\ub124\uc694. \ub9c8\uc9c0\ub9c9 '\uc5b4\ub290\ubd84\uaed8\ub294 \ub3c4\uc6c0\uc774 \ub418\uc168\uae30\ub97c...' \uc774\uac8c \ud56d\uc0c1 \uc640\ub2ff\uc2b5\ub2c8\ub2e4." ] } ], "metadata": {} } ] }