简明python教程 --C++程序员的视角(九):函数式编程、特殊类方法、测试及其他
函數式編程
Lambda
exec,eval和assert語句,repr函數
?
| lambda語句 | 用來創建簡短的單行匿名函數 print_assign = lambda name, value: name + '=' + str(value) 等同于 def print_assign(name, value): return name + '=' + str(value) ?lambda需要一個參數,后面僅跟單個表達式作為函數體,而表達式的值被這個新建的函數返回。注意,即便是print語句也不能用在lambda形式中,只能使用表達式。 >>> ftwice = lambda s:s*2? 再如make_repeater函數在運行時創建新的函數對象,并且返回它。 |
| yield表達式 | 類似于迭代器,生成器允許處理數據序列 不同的是生成器通過使用yi?e?l?d表達式,可以盡量減少程序耗費的內存,同時還能在大規模數據集上提供類似迭代器的功能 |
| map()函數 | 從iterA,iterB...中取出對應元素應用map
map(f,?iterA,?iterB,?...)built-in functions often used with iterators.returns a list containing?f(iterA[0],?iterB[0]),?f(iterA[1],?iterB[1]),?f(iterA[2],?iterB[2]),?.... map(f, iterable)基本上等于[f(x)?for?x?in?iterable] 但在多元的情況下不同:
map()只做了列內運算 >>> map(abc,list1,list2,list3) 列表解析做的是笛卡爾乘積 >>>?[abc(a,b,c) for a in list1 for b in list2 for c in list3] [114477, 114488, 114499, 115577, 115588, 115599, 116677, 116688, 116699, 224477, 224488, 224499, 225577, 225588, 225599, 226677, 226688, 226699, 334477, 334488, 334499, 335577, 335588, 335599, 336677, 336688, 336699] 等同于 result = [] for a in list1: |
| filter()函數 (可以被列表解析替代) | 從iter中選擇滿足predicate條件的元素 filter(predicate,?iter)? returns a list that contains all the sequence elements that meet a certain condition 形式非常簡潔 existing_files = filter(os.path.exists, file_list) ? |
| reduce()函數 | 對iter中每個元素依次兩兩使用func reduce(func,?iter,?[initial_value])func?must be a function that takes two elements and returns a single value.? 先算init=func(initial_value,A)再算func(init,B)再算func(func(init,?B),?C)... |
| sorted()函數 | sorted(iterable,?[cmp=None],?[key=None],?[reverse=False])? 參考 http://wiki.python.org/moin/HowTo/Sorting |
| any()和all()函數 | any()?returns?True?if any element in the iterable is a true value?all()?returns?True?if all of the elements are true values >>> any([1,1,1]) True >>> all([0,1,0]) False |
| Generator expressions | 和list comprehensions的語法稍有不同 ( expression for expr1 in sequence1if condition1...for exprN in sequenceNif conditionN) Output: the successive values of?expression obj_total?=?sum(obj.count?for?obj?in?list_all_objects()) |
| Generators | functions that simplify the task of writing iterators, which |
| functools.partial() | partial function application |
| ? | ? |
| ? | ? |
| exec語句 | 用來執行儲存在字符串或文件中的Python語句。? 例如,可以在運行時生成一個包含Python代碼的字符串,然后使用exec語句執行這些語句。 >>> exec 'print "Hello World"'? |
| eval語句 | 用來計算存儲在字符串中的有效Python表達式。? >>> eval('2*3')? |
| assert語句 | 用來聲明某個條件是真的。? 當assert語句失敗的時候,會引發一個AssertionError。? |
| repr函數 | 用來取得對象的規范字符串表示。? 反引號(也稱轉換符)可以完成相同的功能。? 注意,在大多數時候有eval(repr(object)) == object。? >>> i = ['item']? |
| 函數修飾符 | 通過介入函數的啟動和關閉機制,調整一個已存在函數的行為 |
?
類的特殊方法
| __init__(self,...)?? | 在新建對象恰好要被返回使用之前被調用。 |
| __del__(self)??? | 恰好在對象要被刪除之前調用。 |
| __str__(self)?? | 對對象使用print語句或是使用str()的時候調用。 |
| __lt__(self,other)? | 當使用 小于 運算符(<)的時候調用。類似地,對于所有的運算符(+,>等等)都有特殊的方法。 |
| __getitem__(self,key)??? | 使用x[key]索引操作符的時候調用。 |
| __len__(self) | 對序列對象使用內建的len()函數的時候調用。 ? |
測試
?對于規模更大的程序,測試框架必不可少。
在標準庫中,unittest模塊基于流行的xunit測試框架。doctest允許從shell會話取得輸入。
?
?
參考:
Python中map()函數淺析?
http://docs.python.org/2/howto/functional.html
Python特殊語法:filter、map、reduce、lambda、yield
http://www.dataguru.cn/blog-10716-1133.html
http://jgy3.ggclk.com/url?url=http%3A%2F%2Fmikecvet.wordpress.com%2F2010%2F07%2F02%2Fparallel-mapreduce-in-python%2F&v=4&i=6&q=map%20python%20lambda%20%20yield%20reduce%20filter&p=0&tr=0&at=0&ar=0&ab=0&mr=0&ir=0&kgr=0&nr=0&iar=0&sr=0
?
from:?http://www.cnblogs.com/wei-li/p/3439182.html
總結
以上是生活随笔為你收集整理的简明python教程 --C++程序员的视角(九):函数式编程、特殊类方法、测试及其他的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简明python教程 --C++程序员的
- 下一篇: OpenCV Harris 角点检测子