【python编码最佳实践之总结】
Saturday, July 2, 2016
5:02 PM
|
| | —- |
| Tags: #微博 |

IT程序猿
07/02/2016
【python编码最佳实践之总结】相信用python的同学不少,本人也一直对python情有独钟,毫无疑问python作为一门解释性动态语言没有那些编译型语言高效,但是python简洁、易读以及可扩展性等特性使得它大受青睐。http://t.cn/Rbu1y7X(来自: 云栖社区 )
![计算机生成了可选文字: ( ATA) : python , (EGG. ( ATA ) python , ? python —fi : There should be one— and preferably only one —obvious way to do it.Although that way may not be obvious at first unless you’re Dutch. Qruby , , 3. collections i. deque : ii. defaultdict : iii. namedtuple : dict.setdefaultgÄ , SIEpython2ßÄ5—frf51J+ > > > from pbp. scripts. profiler import profile, stats [ yellow’, (‘blue’, 2), yellow’, 3), (‘blue’, 4), red’, > > > O profile defaultdict’) def faster(): — defaultdict(list) for k, v in s: d [k] . append (v) > > > •profile dict’) def slower(): for k, v in s: d. setdefault (k, append (v) > > > slower(), faster() Optimization: Solutions [ 306 ] > > > stats dict’] I’ stones’ 16.587882671716077, o. 351663112640380861 ‘ time’ > > > stats default dict’] I’ stones’ 6. 5733464259021686, o. 139354944229125981 ‘ time’ memory memory 396, 552, , , EEfilongtg the following is not so Pythonic numb ers range (10) evens len (numbers): while i < if i evens. append (i) [0, 8] the good way to iterate a range, elegant a nd efficient > > > evens [0, _f51J2 . def _treament (pos, return ‘ for i in range (10) if 8] element) % (pos, element) open name list print . split()) _list pr int ange _fun c print _list print _list print ge(IO))] output test. txt’ , comps I for line in f for word in line comps 2 y + I) for x in range (3) for y in r — O, range (10)) filter (lambda x: x 2 c o mps 3 [i for i in range (10) if i % 2 comps4 pythonic [_treament (i, el) for i, el in enumerate (ran 1), (1, 2), (1, 3), (2, 1), (2, 2), (3, 3), 2), (2, 4), [0, [0, (3, 8] 8] , Sl#python open / etc/motd, longest = max (len(x. strip()) for x in f) f. close() return longest Ehmemoizationfi , import time import hashlib import pickle from itertools import cache def is_obsolete (entry, return time. time ( ) chain duration) entry time’] durati on def compute key (functi on, args, kw) pickle pickle. dumps ( (function. funcname, args, kw)) key hashiib-æ— return hashlib. shal (key) hex digest() def memoize (duration—I O): def _memoize (function) memoize (args, def compute_key (function, key do we have it already if (key in cache and args, not is_obsolete (cache [key print ‘we got a winner return cache [key] value’] computing — functi on (_args, result duration)) storing the result cache [k ey I’ value’ ‘ time’ return result memo i ze return return memo i ze Omemoize ( ) def (a, b, return a + b + c print (2, print (2, Omemoize(l) def (a, b): return a + b print (2, time. sleep (2) print (2, 6 we got a winner 6 4 4 result, time. time ( ) I c): 2) 2) 2) 2) 3. #7python , 1. fifiÆükEä5E±lä5iterator ) if done is not NonebEEif done ! = None 2. , for i in seq: 3. print i ‘x < y < z’ft‘x < y and y < z’ , 4. while IQEwhile 5. 6. ä5ht , 8. y, x elegant and efficient temp temp 9. ( python2.5E) : VI if X else V 2 , and VI) or V2 , , 10. pythonZswitch caseÄ : %switch , FfiVIpythonä.tz,Q fiswitch switch case switch (var) case v case v 2: case v N. default: f uncl ( ) ; func2(), funcN() default_func ( ) dictionarym values values. get (var, lambda: Iamb da: Iamb da: I value] ( ) funcl, func2, funcN, default_func) ( ) funcl, func2, fun c 3 , i_Eä , pythonic ! . http://t.cn/RbuvpV6
查看源微博
已使用 Microsoft OneNote 2016 创建。
