Python Closures and Decorators (Part. 2)
In part 1, we looked at sending functions as arguments to other functions, at nesting functinons, and finally we wrapped a function in another function. We'll begin this part by giving an example implementation on the exercise I gave in part 1:, line 11)
02.
03.Unknown directive type "code".
04.
05... code:: python
06.>>> def print_call(fn):
07.... def fn_wrap(*args, **kwargs):
08.... print("Calling %s with arguments: \n\targs: %s\n\tkwargs:%s" % (
09.... fn.__name__, args, kwargs))
10.... retval = fn(*args, **kwargs)
11.... print("%s returning '%s'" % (fn.func_name, retval))
12.... return retval
13.... fn_wrap.func_name = fn.func_name
14.... return fn_wrap
15....
16.>>> def greeter(greeting, what='world'):
17.... return "%s %s!" % (greeting, what)
18....
19.>>> greeter = print_call(greeter)
20.>>> greeter("Hi")
21.Calling greeter with arguments:
22.args: ('Hi',)
23.kwargs:{}
24.greeter returning 'Hi world!'
25.'Hi world!'
26.>>> greeter("Hi", what="Python")
27.Calling greeter with arguments:
28.args: ('Hi',)
29.kwargs:{'what': 'Python'}
30.greeter returning 'Hi Python!'
31.'Hi Python!'
32.>>>So, this is at least mildly useful, but it'll get better! You may or may not have heard of closures, and you may have heard any of a large number of defenitions of what a closure is - I won't go into nitpicking, but just say that a closure is a block of code (for example a function) that captures (or closes over) non-local (free) variables. If this is all gibberish to you, you're probably in need of a CS refresher, but fear not - I'll show by example, and the concept is easy enough to understand: a function can reference variables that are defined in the function's enclosing scope.
CODE:
01.System Message: ERROR/3 (02.
03.Unknown directive type "code".
04.
05... code:: python
06.>>> def print_call(fn):
07.... def fn_wrap(*args, **kwargs):
08.... print("Calling %s with arguments: \n\targs: %s\n\tkwargs:%s" % (
09.... fn.__name__, args, kwargs))
10.... retval = fn(*args, **kwargs)
11.... print("%s returning '%s'" % (fn.func_name, retval))
12.... return retval
13.... fn_wrap.func_name = fn.func_name
14.... return fn_wrap
15....
16.>>> def greeter(greeting, what='world'):
17.... return "%s %s!" % (greeting, what)
18....
19.>>> greeter = print_call(greeter)
20.>>> greeter("Hi")
21.Calling greeter with arguments:
22.args: ('Hi',)
23.kwargs:{}
24.greeter returning 'Hi world!'
25.'Hi world!'
26.>>> greeter("Hi", what="Python")
27.Calling greeter with arguments:
28.args: ('Hi',)
29.kwargs:{'what': 'Python'}
30.greeter returning 'Hi Python!'
31.'Hi Python!'
32.>>>So, this is at least mildly useful, but it'll get better! You may or may not have heard of closures, and you may have heard any of a large number of defenitions of what a closure is - I won't go into nitpicking, but just say that a closure is a block of code (for example a function) that captures (or closes over) non-local (free) variables. If this is all gibberish to you, you're probably in need of a CS refresher, but fear not - I'll show by example, and the concept is easy enough to understand: a function can reference variables that are defined in the function's enclosing scope.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734619/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Angular 2 Decorators – 1Angular
- ECMAScript Decorators---裝飾器
- 【低門檻 手把手】python 裝飾器(Decorators)原理說明Python
- Verlet integration 韋爾萊積分 (Part. 1)
- Draft 文件翻譯 - 高階主題 - DecoratorsRaft
- 聊聊Typescript中的設計模式——裝飾器篇(decorators)TypeScript設計模式
- python爬蟲2Python爬蟲
- (2)python引數Python
- Python-day2Python
- python載入2Python
- Python爬蟲--2Python爬蟲
- python學習2Python
- fluent python 讀書筆記 2–Python的序列型別2Python筆記型別
- Python 筆記-2-1-Python 概述Python筆記
- python2到python3程式碼轉化:2to3Python
- Python 基礎 2 - 列表Python
- 第2章 Python序列Python
- python資料庫2Python資料庫
- Python2轉Python3比較Python
- Python基礎:第一個Python程式(2)Python
- Python2升級Python3(1):xrangePython
- windows下 相容Python2和Python3WindowsPython
- Python2和Python3的區別Python
- Python2與Python3的區別Python
- python基礎學習2Python
- 我認識的python(2)Python
- Python相關語法2Python
- Python學習筆記(2)Python筆記
- python2 &&pssh 安裝Python
- python Math.atan2() degrees()Python
- Python入門:Python 2與Python3有什麼區別?Python
- 學習Python選擇Python2還是Python3呢?Python
- 讓pip使用python3而不是python2Python
- python2.x和python3.x區別Python
- Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()PythonOpenCV
- python 裝飾器 part2Python
- python3: 字串和文字(2)Python字串
- python基礎7 - 函式2Python函式
- python2的朋友們注意!!!Python