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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python Closures and DecoratorsPython
- Python高階特性(2):Closures、Decorators和functoolsPython
- Angular 2 Decorators – 1Angular
- Closures for JavaJava
- The Little JavaScript ClosuresJavaScript
- Javascript 閉包(Closures)JavaScript
- ES Decorators簡介
- ECMAScript Decorators---裝飾器
- Demystifying JavaScript Closures, Callbacks and IIFEsJavaScript
- 【低門檻 手把手】python 裝飾器(Decorators)原理說明Python
- Draft 文件翻譯 - 高階主題 - DecoratorsRaft
- 學習一下閉包函式 - Closures函式
- REST : rest_framework.decorators.api_view 實現PATCHRESTFrameworkAPIView
- 使用 ES decorators 構建一致性 APIAPI
- 聊聊Typescript中的設計模式——裝飾器篇(decorators)TypeScript設計模式
- python2Python
- Python-day2Python
- python爬蟲2Python爬蟲
- (2)python引數Python
- Python學習(2)Python
- python基礎2Python
- python tips(2)Python
- python筆記2Python筆記
- python載入2Python
- Python爬蟲--2Python爬蟲
- python學習2Python
- fluent python 讀書筆記 2–Python的序列型別2Python筆記型別
- python2到python3程式碼轉化:2to3Python
- fluent python 讀書筆記 2--Python的序列型別2Python筆記型別
- Python 筆記-2-1-Python 概述Python筆記
- Python 基礎 2 - 列表Python
- 第2章 Python序列Python
- python資料庫2Python資料庫
- 逃離 Python2Python
- python之numpy庫[2]Python
- Selenium2 (python)Python
- 用Python解析XML(2)PythonXML
- Python基礎:第一個Python程式(2)Python