Python璇█鐗规��
1 Python鐨勫嚱鏁板弬鏁颁紶閫�
鐪嬩袱涓涓嬩緥瀛愶紝鍒嗘瀽杩愯缁撴灉:
浠g爜涓�锛�
a = 1
def fun(a):
聽聽聽聽a = 2
fun(a)
print(a)聽聽# 1
浠g爜浜岋細
a = []
def fun(a):
a.append(1)
fun(a)
print(a)聽聽# [1]
鎵�鏈夌殑鍙橀噺閮藉彲浠ョ悊瑙f槸鍐呭瓨涓竴涓璞$殑鈥滃紩鐢ㄢ�濓紝鎴栬�咃紝涔熷彲浠ョ湅浼糲涓璿oid*鐨勬劅瑙夈��
杩欓噷璁颁綇鐨勬槸绫诲瀷鏄睘浜庡璞$殑锛岃�屼笉鏄彉閲忋�傝�屽璞℃湁涓ょ,鈥滃彲鏇存敼鈥濓紙mutable锛変笌鈥滀笉鍙洿鏀光�濓紙immutable锛夊璞°�傚湪python涓紝strings, tuples, 鍜宯umbers鏄笉鍙洿鏀圭殑瀵硅薄锛岃�宭ist,dict绛夊垯鏄彲浠ヤ慨鏀圭殑瀵硅薄銆�(杩欏氨鏄繖涓棶棰樼殑閲嶇偣)
褰撲竴涓紩鐢ㄤ紶閫掔粰鍑芥暟鐨勬椂鍊�,鍑芥暟鑷姩澶嶅埗涓�浠藉紩鐢�,杩欎釜鍑芥暟閲岀殑寮曠敤鍜屽杈圭殑寮曠敤娌℃湁鍗婃瘺鍏崇郴浜�.鎵�浠ョ涓�涓緥瀛愰噷鍑芥暟鎶婂紩鐢ㄦ寚鍚戜簡涓�涓笉鍙彉瀵硅薄,褰撳嚱鏁拌繑鍥炵殑鏃跺��,澶栭潰鐨勫紩鐢ㄦ病鍗婃瘺鎰熻.鑰岀浜屼釜渚嬪瓙灏变笉涓�鏍蜂簡,鍑芥暟鍐呯殑寮曠敤鎸囧悜鐨勬槸鍙彉瀵硅薄,瀵瑰畠鐨勬搷浣滃氨鍜屽畾浣嶄簡鎸囬拡鍦板潃涓�鏍�,鍦ㄥ唴瀛橀噷杩涜淇敼.
2 Python涓殑鍏冪被(metaclass)
鍏冪被灏辨槸鐢ㄦ潵鍒涘缓绫荤殑鈥滀笢瑗库�濄�備綘鍒涘缓绫诲氨鏄负浜嗗垱寤虹被鐨勫疄渚嬪璞★紝浣嗘槸鎴戜滑宸茬粡瀛︿範鍒颁簡Python涓殑绫讳篃鏄璞°�傚ソ鍚э紝鍏冪被灏辨槸鐢ㄦ潵鍒涘缓杩欎簺绫伙紙瀵硅薄锛夌殑锛屽厓绫诲氨鏄被鐨勭被
杩欎釜闈炲父鐨勪笉甯哥敤,璇︽儏璇风湅锛氥��娣卞埢鐞嗚ВPython涓殑鍏冪被(metaclass)銆�
3 @staticmethod鍜孈classmethod
Python鍏跺疄鏈�3涓柟娉�,鍗抽潤鎬佹柟娉�(staticmethod),绫绘柟娉�(classmethod)鍜屽疄渚嬫柟娉�,濡備笅:
class A(object):
聽聽聽聽def foo(self,x):
聽聽聽聽聽聽聽聽print "executing foo(%s,%s)"%(self,x)
聽聽聽聽@classmethod
聽聽聽聽def class_foo(cls,x):
聽聽聽聽聽聽聽聽print(聽"executing class_foo(%s,%s)"%(cls,x))
聽聽聽聽@staticmethod
聽聽聽聽def static_foo(x):
聽聽聽聽聽聽聽聽print ("executing static_foo(%s)"%x)
a=A()
杩欓噷鍏堢悊瑙d笅鍑芥暟鍙傛暟閲岄潰鐨剆elf鍜宑ls.杩欎釜self鍜宑ls鏄绫绘垨鑰呭疄渚嬬殑缁戝畾.瀵逛簬瀹炰緥鏂规硶,鎴戜滑鐭ラ亾鍦ㄧ被閲屾瘡娆″畾涔夋柟娉曠殑鏃跺�欓兘闇�瑕佺粦瀹氳繖涓疄渚�,灏辨槸foo(self, x),涓轰粈涔堣杩欎箞鍋氬憿?鍥犱负瀹炰緥鏂规硶鐨勮皟鐢ㄧ涓嶅紑瀹炰緥,鎴戜滑闇�瑕佹妸瀹炰緥鑷繁浼犵粰鍑芥暟,璋冪敤鐨勬椂鍊欐槸杩欐牱鐨刟.foo(x)(鍏跺疄鏄痜oo(a, x)).绫绘柟娉曚竴鏍�,鍙笉杩囧畠浼犻�掔殑鏄被鑰屼笉鏄疄渚�,A.class_foo(x).娉ㄦ剰杩欓噷鐨剆elf鍜宑ls鍙互鏇挎崲鍒殑鍙傛暟,浣嗘槸python鐨勭害瀹氭槸杩欎咯,杩樻槸涓嶈鏀圭殑濂�.
瀵逛簬闈欐�佹柟娉曞叾瀹炲拰鏅�氱殑鏂规硶涓�鏍�,涓嶉渶瑕佸璋佽繘琛岀粦瀹�,鍞竴鐨勫尯鍒槸璋冪敤鐨勬椂鍊欓渶瑕佷娇鐢╝.static_foo(x)鎴栬�匒.static_foo(x)鏉ヨ皟鐢�.
\瀹炰緥鏂规硶绫绘柟娉�闈欐�佹柟娉�
a = A()a.foo(x)a.class_foo(x)a.static_foo(x)
A涓嶅彲鐢ˋ.class_foo(x)A.static_foo(x)
4 绫诲彉閲忓拰瀹炰緥鍙橀噺
聽class Person:
聽聽聽聽name="aaa"
p1=Person()
p2=Person()
p1.name="bbb"
print(p1.name)聽聽# bbb
print(p2.name)聽聽# aaa
print(Person.name)聽聽# aaa
绫诲彉閲忓氨鏄緵绫讳娇鐢ㄧ殑鍙橀噺,瀹炰緥鍙橀噺灏辨槸渚涘疄渚嬩娇鐢ㄧ殑.
杩欓噷p1.name="bbb"鏄疄渚嬭皟鐢ㄤ簡绫诲彉閲�,杩欏叾瀹炲拰涓婇潰绗竴涓棶棰樹竴鏍�,灏辨槸鍑芥暟浼犲弬鐨勯棶棰�,p1.name涓�寮�濮嬫槸鎸囧悜鐨勭被鍙橀噺name="aaa",浣嗘槸鍦ㄥ疄渚嬬殑浣滅敤鍩熼噷鎶婄被鍙橀噺鐨勫紩鐢ㄦ敼鍙樹簡,灏卞彉鎴愪簡涓�涓疄渚嬪彉閲�,self.name涓嶅啀寮曠敤Person鐨勭被鍙橀噺name浜�.
鍙互鐪嬬湅涓嬮潰鐨勪緥瀛�:
聽class Person:
聽聽聽聽name=[]
p1=Person()
p2=Person()
p1.name.append(1)
print(p1.name)聽聽# [1]
print(p2.name)聽聽# [1]
print(Person.name)聽聽# [1]
5 Python鑷渷
杩欎釜涔熸槸python褰倣鐨勭壒鎬�.
鑷渷灏辨槸闈㈠悜瀵硅薄鐨勮瑷�鎵�鍐欑殑绋嬪簭鍦ㄨ繍琛屾椂,鎵�鑳界煡閬撳璞$殑绫诲瀷.绠�鍗曚竴鍙ュ氨鏄繍琛屾椂鑳藉鑾峰緱瀵硅薄鐨勭被鍨�.姣斿type(),dir(),getattr(),hasattr(),isinstance().
6 瀛楀吀鎺ㄥ寮�
鍙兘浣犺杩囧垪琛ㄦ帹瀵兼椂,鍗存病鏈夎杩囧瓧鍏告帹瀵煎紡,鍦�2.7涓墠鍔犲叆鐨�:
聽d = {key: value for (key, value) in iterable}
7 Python涓崟涓嬪垝绾垮拰鍙屼笅鍒掔嚎
>>> class MyClass():
...聽聽聽聽 def __init__(self):
...聽聽聽聽聽聽聽聽聽聽聽聽 self.__superprivate = "Hello"
...聽聽聽聽聽聽聽聽聽聽聽聽 self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print(mc.__superprivate)
Traceback (most recent call last):
聽聽File "", line 1, in
AttributeError: myClass instance has no attribute '__superprivate'
>>> print(mc._semiprivate)
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}
__foo__:涓�绉嶇害瀹�,Python鍐呴儴鐨勫悕瀛�,鐢ㄦ潵鍖哄埆鍏朵粬鐢ㄦ埛鑷畾涔夌殑鍛藉悕,浠ラ槻鍐茬獊.
_foo:涓�绉嶇害瀹�,鐢ㄦ潵鎸囧畾鍙橀噺绉佹湁.绋嬪簭鍛樼敤鏉ユ寚瀹氱鏈夊彉閲忕殑涓�绉嶆柟寮�.
__foo:杩欎釜鏈夌湡姝g殑鎰忎箟:瑙f瀽鍣ㄧ敤_classname__foo鏉ヤ唬鏇胯繖涓悕瀛�,浠ュ尯鍒拰鍏朵粬绫荤浉鍚岀殑鍛藉悕.
璇︽儏瑙�:
http://www.zhihu.com/question/19754941
8 瀛楃涓叉牸寮忓寲:%鍜�.format
.format鍦ㄨ澶氭柟闈㈢湅璧锋潵鏇翠究鍒�.瀵逛簬%鏈�鐑︿汉鐨勬槸瀹冩棤娉曞悓鏃朵紶閫掍竴涓彉閲忓拰鍏冪粍.浣犲彲鑳戒細鎯充笅闈㈢殑浠g爜涓嶄細鏈変粈涔堥棶棰�:
Python:
聽"hi there %s" % name
浣嗘槸,濡傛灉name鎭板ソ鏄�(1,2,3),瀹冨皢浼氭姏鍑轰竴涓猅ypeError寮傚父.涓轰簡淇濊瘉瀹冩�绘槸姝g‘鐨�,浣犲繀椤昏繖鏍峰仛:
聽"hi there %s" % (name,)聽聽 # 鎻愪緵涓�涓崟鍏冪礌鐨勬暟缁勮�屼笉鏄竴涓弬鏁�
9 杩唬鍣ㄥ拰鐢熸垚鍣�
鍦≒ython涓紝杩欑涓�杈瑰惊鐜竴杈硅绠楃殑鏈哄埗锛岀О涓虹敓鎴愬櫒锛歡enerator銆�
鍙互琚玭ext()鍑芥暟璋冪敤骞朵笉鏂繑鍥炰笅涓�涓�肩殑瀵硅薄绉颁负杩唬鍣細Iterator銆�
杩欎釜鏄痵tackoverflow閲宲ython鎺掑悕绗竴鐨勯棶棰�,鍊煎緱涓�鐪�: http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python
10聽*args聽and聽**kwargs
鐢�*args鍜�**kwargs鍙槸涓轰簡鏂逛究骞舵病鏈夊己鍒朵娇鐢ㄥ畠浠�.
褰撲綘涓嶇‘瀹氫綘鐨勫嚱鏁伴噷灏嗚浼犻�掑灏戝弬鏁版椂浣犲彲浠ョ敤*args.渚嬪,瀹冨彲浠ヤ紶閫掍换鎰忔暟閲忕殑鍙傛暟:
>>> def print_everything(*args):
聽聽聽聽聽聽聽聽for count, thing in enumerate(args):
...聽聽聽聽聽聽聽聽 print '{0}. {1}'.format(count, thing)
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage
鐩镐技鐨�,**kwargs鍏佽浣犱娇鐢ㄦ病鏈変簨鍏堝畾涔夌殑鍙傛暟鍚�:
>>> def table_things(**kwargs):
...聽聽聽聽 for name, value in kwargs.items():
...聽聽聽聽聽聽聽聽 print '{0} = {1}'.format(name, value)
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit
浣犱篃鍙互娣风潃鐢�.鍛藉悕鍙傛暟棣栧厛鑾峰緱鍙傛暟鍊肩劧鍚庢墍鏈夌殑鍏朵粬鍙傛暟閮戒紶閫掔粰*args鍜�**kwargs.鍛藉悕鍙傛暟鍦ㄥ垪琛ㄧ殑鏈�鍓嶇.渚嬪:
1def table_things(titlestring, **kwargs)
*args鍜�**kwargs鍙互鍚屾椂鍦ㄥ嚱鏁扮殑瀹氫箟涓�,浣嗘槸*args蹇呴』鍦�**kwargs鍓嶉潰.
褰撹皟鐢ㄥ嚱鏁版椂浣犱篃鍙互鐢�*鍜�**璇硶.渚嬪:
>>> def print_three_things(a, b, c):
...聽聽聽聽 print 'a = {0}, b = {1}, c = {2}'.format(a,b,c)
...
>>> mylist = ['aardvark', 'baboon', 'cat']
>>> print_three_things(*mylist)
a = aardvark, b = baboon, c = cat
灏卞儚浣犵湅鍒扮殑涓�鏍�,瀹冨彲浠ヤ紶閫掑垪琛�(鎴栬�呭厓缁�)鐨勬瘡涓�椤瑰苟鎶婂畠浠В鍖�.娉ㄦ剰蹇呴』涓庡畠浠湪鍑芥暟閲岀殑鍙傛暟鐩稿惢鍚�.褰撶劧,浣犱篃鍙互鍦ㄥ嚱鏁板畾涔夋垨鑰呭嚱鏁拌皟鐢ㄦ椂鐢�*.
http://stackoverflow.com/questions/3394835/args-and-kwargs
11 闈㈠悜鍒囬潰缂栫▼AOP鍜岃楗板櫒
杩欎釜AOP涓�鍚捣鏉ユ湁鐐规嚨,鍚屽闈㈣瘯鐨勬椂鍊欏氨琚棶鎳典簡鈥�
瑁呴グ鍣ㄦ槸涓�涓緢钁楀悕鐨勮璁℃ā寮忥紝缁忓父琚敤浜庢湁鍒囬潰闇�姹傜殑鍦烘櫙锛岃緝涓虹粡鍏哥殑鏈夋彃鍏ユ棩蹇椼�佹�ц兘娴嬭瘯銆佷簨鍔″鐞嗙瓑銆傝楗板櫒鏄В鍐宠繖绫婚棶棰樼殑缁濅匠璁捐锛屾湁浜嗚楗板櫒锛屾垜浠氨鍙互鎶界鍑哄ぇ閲忓嚱鏁颁腑涓庡嚱鏁板姛鑳芥湰韬棤鍏崇殑闆峰悓浠g爜骞剁户缁噸鐢ㄣ�傛鎷殑璁诧紝瑁呴グ鍣ㄧ殑浣滅敤灏辨槸涓哄凡缁忓瓨鍦ㄧ殑瀵硅薄娣诲姞棰濆鐨勫姛鑳姐��
杩欎釜闂姣旇緝澶�,鎺ㄨ崘: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python
涓枃: http://taizilongxu.gitbooks.io/stackoverflow-about-python/content/3/README.html
12 楦瓙绫诲瀷
鈥滃綋鐪嬪埌涓�鍙笩璧拌捣鏉ュ儚楦瓙銆佹父娉宠捣鏉ュ儚楦瓙銆佸彨璧锋潵涔熷儚楦瓙锛岄偅涔堣繖鍙笩灏卞彲浠ヨ绉颁负楦瓙銆傗��
鎴戜滑骞朵笉鍏冲績瀵硅薄鏄粈涔堢被鍨嬶紝鍒板簳鏄笉鏄腑瀛愶紝鍙叧蹇冭涓恒��
姣斿鍦╬ython涓紝鏈夊緢澶歠ile-like鐨勪笢瑗匡紝姣斿StringIO,GzipFile,socket銆傚畠浠湁寰堝鐩稿悓鐨勬柟娉曪紝鎴戜滑鎶婂畠浠綋浣滄枃浠朵娇鐢ㄣ��
鍙堟瘮濡俵ist.extend()鏂规硶涓�,鎴戜滑骞朵笉鍏冲績瀹冪殑鍙傛暟鏄笉鏄痩ist,鍙瀹冩槸鍙凯浠g殑,鎵�浠ュ畠鐨勫弬鏁板彲浠ユ槸list/tuple/dict/瀛楃涓�/鐢熸垚鍣ㄧ瓑.
楦瓙绫诲瀷鍦ㄥ姩鎬佽瑷�涓粡甯镐娇鐢紝闈炲父鐏垫椿锛屼娇寰梡ython涓嶆兂java閭f牱涓撻棬鍘诲紕涓�澶у爢鐨勮璁℃ā寮忋��
13 Python涓噸杞�
寮曡嚜鐭ヤ箮:http://www.zhihu.com/question/20053359
鍑芥暟閲嶈浇涓昏鏄负浜嗚В鍐充袱涓棶棰樸��
[if !supportLists]1.聽[endif]鍙彉鍙傛暟绫诲瀷銆�
[if !supportLists]2.聽[endif]鍙彉鍙傛暟涓暟銆�
鍙﹀锛屼竴涓熀鏈殑璁捐鍘熷垯鏄紝浠呬粎褰撲袱涓嚱鏁伴櫎浜嗗弬鏁扮被鍨嬪拰鍙傛暟涓暟涓嶅悓浠ュ锛屽叾鍔熻兘鏄畬鍏ㄧ浉鍚岀殑锛屾鏃舵墠浣跨敤鍑芥暟閲嶈浇锛屽鏋滀袱涓嚱鏁扮殑鍔熻兘鍏跺疄涓嶅悓锛岄偅涔堜笉搴斿綋浣跨敤閲嶈浇锛岃�屽簲褰撲娇鐢ㄤ竴涓悕瀛椾笉鍚岀殑鍑芥暟銆�
濂藉惂锛岄偅涔堝浜庢儏鍐�1 锛屽嚱鏁板姛鑳界浉鍚岋紝浣嗘槸鍙傛暟绫诲瀷涓嶅悓锛宲ython 濡備綍澶勭悊锛熺瓟妗堟槸鏍规湰涓嶉渶瑕佸鐞嗭紝鍥犱负 python 鍙互鎺ュ彈浠讳綍绫诲瀷鐨勫弬鏁帮紝濡傛灉鍑芥暟鐨勫姛鑳界浉鍚岋紝閭d箞涓嶅悓鐨勫弬鏁扮被鍨嬪湪 python 涓緢鍙兘鏄浉鍚岀殑浠g爜锛屾病鏈夊繀瑕佸仛鎴愪袱涓笉鍚屽嚱鏁般��
閭d箞瀵逛簬鎯呭喌2 锛屽嚱鏁板姛鑳界浉鍚岋紝浣嗗弬鏁颁釜鏁颁笉鍚岋紝python 濡備綍澶勭悊锛熷ぇ瀹剁煡閬擄紝绛旀灏辨槸缂虹渷鍙傛暟銆傚閭d簺缂哄皯鐨勫弬鏁拌瀹氫负缂虹渷鍙傛暟鍗冲彲瑙e喅闂銆傚洜涓轰綘鍋囪鍑芥暟鍔熻兘鐩稿悓锛岄偅涔堥偅浜涚己灏戠殑鍙傛暟缁堝綊鏄渶瑕佺敤鐨勩��
濂戒簡锛岄壌浜庢儏鍐�1 璺� 鎯呭喌 2 閮芥湁浜嗚В鍐虫柟妗堬紝python 鑷劧灏变笉闇�瑕佸嚱鏁伴噸杞戒簡銆�
14 鏂板紡绫诲拰鏃у紡绫�
杩欎釜闈㈣瘯瀹橀棶浜�,鎴戣浜嗚�佸崐澶�,涓嶇煡閬撲粬闂殑鐪熸鎰忓浘鏄粈涔�.
杩欑瘒鏂囩珷寰堝ソ鐨勪粙缁嶄簡鏂板紡绫荤殑鐗规��: http://www.cnblogs.com/btchenguang/archive/2012/09/17/2689146.html
鏂板紡绫诲緢鏃╁湪2.2灏卞嚭鐜颁簡,鎵�浠ユ棫寮忕被瀹屽叏鏄吋瀹圭殑闂,Python3閲岀殑绫诲叏閮ㄩ兘鏄柊寮忕被.杩欓噷鏈変竴涓狹RO闂鍙互浜嗚В涓�(鏂板紡绫绘槸骞垮害浼樺厛,鏃у紡绫绘槸娣卞害浼樺厛),閲岃鐨勪篃寰堝.
15聽__new__鍜�__init__鐨勫尯鍒�
杩欎釜__new__纭疄寰堝皯瑙佸埌,鍏堝仛浜嗚В鍚�.
[if !supportLists]1.聽[endif]__new__鏄竴涓潤鎬佹柟娉�,鑰宊_init__鏄竴涓疄渚嬫柟娉�.
[if !supportLists]2.聽[endif]__new__鏂规硶浼氳繑鍥炰竴涓垱寤虹殑瀹炰緥,鑰宊_init__浠�涔堥兘涓嶈繑鍥�.
[if !supportLists]3.聽[endif]鍙湁鍦╛_new__杩斿洖涓�涓猚ls鐨勫疄渚嬫椂鍚庨潰鐨刜_init__鎵嶈兘琚皟鐢�.
[if !supportLists]4.聽[endif]褰撳垱寤轰竴涓柊瀹炰緥鏃惰皟鐢╛_new__,鍒濆鍖栦竴涓疄渚嬫椂鐢╛_init__.
ps:聽__metaclass__鏄垱寤虹被鏃惰捣浣滅敤.鎵�浠ユ垜浠彲浠ュ垎鍒娇鐢╛_metaclass__,__new__鍜宊_init__鏉ュ垎鍒湪绫诲垱寤�,瀹炰緥鍒涘缓鍜屽疄渚嬪垵濮嬪寲鐨勬椂鍊欏仛涓�浜涘皬鎵嬭剼.
16 鍗曚緥妯″紡
杩欎釜缁濆甯歌�冨晩.缁濆瑕佽浣�1~2涓柟娉�,褰撴椂闈㈣瘯瀹樻槸璁╂墜鍐欑殑.
1浣跨敤__new__鏂规硶
聽class聽Singleton(object):
聽聽聽聽def聽__new__(cls,聽*args,聽**kw):
聽聽聽聽聽聽聽聽if聽not聽hasattr(cls,聽'_instance'):
聽聽聽聽聽聽聽聽聽聽聽聽orig聽=聽super(Singleton,聽cls)
聽聽聽聽聽聽聽聽聽聽聽聽cls._instance聽=聽orig.__new__(cls,聽*args,聽**kw)
聽聽聽聽聽聽聽聽return聽cls._instance
class聽MyClass(Singleton):
聽聽聽聽a聽=聽1
2鍏变韩灞炴��
鍒涘缓瀹炰緥鏃舵妸鎵�鏈夊疄渚嬬殑__dict__鎸囧悜鍚屼竴涓瓧鍏�,杩欐牱瀹冧滑鍏锋湁鐩稿悓鐨勫睘鎬у拰鏂规硶.
class聽Borg(object):
聽聽聽聽_state聽=聽{}
聽聽聽聽def聽__new__(cls,聽*args,聽**kw):
聽聽聽聽聽聽聽聽ob聽=聽super(Borg,聽cls).__new__(cls,聽*args,聽**kw)
聽聽聽聽聽聽聽聽ob.__dict__聽=聽cls._state
聽聽聽聽聽聽聽聽return聽ob
class聽MyClass2(Borg):
聽聽聽聽a聽=聽1
3瑁呴グ鍣ㄧ増鏈�
def聽singleton(cls,聽*args,聽**kw):
聽聽聽聽instances聽=聽{}
聽聽聽聽def聽getinstance():
聽聽聽聽聽聽聽聽if聽cls not聽in聽instances:
聽聽聽聽聽聽聽聽聽聽聽聽instances[cls]聽=聽cls(*args,聽**kw)
聽聽聽聽聽聽聽聽return聽instances[cls]
聽聽聽聽return聽getinstance
@singleton
class聽MyClass:
4 import鏂规硶
浣滀负python鐨勬ā鍧楁槸澶╃劧鐨勫崟渚嬫ā寮�
聽# mysingleton.py
class聽My_Singleton(object):
聽聽聽聽def聽foo(self):
聽聽聽聽聽聽聽聽pass
my_singleton聽=聽My_Singleton()
# to use
from聽mysingleton import聽my_singleton
my_singleton.foo()
17 Python涓殑浣滅敤鍩�
Python 涓紝涓�涓彉閲忕殑浣滅敤鍩熸�绘槸鐢卞湪浠g爜涓璧嬪�肩殑鍦版柟鎵�鍐冲畾鐨勩��
褰揚ython 閬囧埌涓�涓彉閲忕殑璇濅粬浼氭寜鐓ц繖鏍风殑椤哄簭杩涜鎼滅储锛�
鏈湴浣滅敤鍩燂紙Local锛夆啋褰撳墠浣滅敤鍩熻宓屽叆鐨勬湰鍦颁綔鐢ㄥ煙锛圗nclosing locals锛夆啋鍏ㄥ眬/妯″潡浣滅敤鍩燂紙Global锛夆啋鍐呯疆浣滅敤鍩燂紙Built-in锛�
18 GIL绾跨▼鍏ㄥ眬閿�
绾跨▼鍏ㄥ眬閿�(Global Interpreter Lock),鍗砅ython涓轰簡淇濊瘉绾跨▼瀹夊叏鑰岄噰鍙栫殑鐙珛绾跨▼杩愯鐨勯檺鍒�,璇寸櫧浜嗗氨鏄竴涓牳鍙兘鍦ㄥ悓涓�鏃堕棿杩愯涓�涓嚎绋�.
瑙e喅鍔炴硶灏辨槸澶氳繘绋嬪拰涓嬮潰鐨勫崗绋�(鍗忕▼涔熷彧鏄崟CPU,浣嗘槸鑳藉噺灏忓垏鎹唬浠锋彁鍗囨�ц兘).
19 鍗忕▼
绠�鍗曠偣璇村崗绋嬫槸杩涚▼鍜岀嚎绋嬬殑鍗囩骇鐗�,杩涚▼鍜岀嚎绋嬮兘闈复鐫�鍐呮牳鎬佸拰鐢ㄦ埛鎬佺殑鍒囨崲闂鑰岃�楄垂璁稿鍒囨崲鏃堕棿,鑰屽崗绋嬪氨鏄敤鎴疯嚜宸辨帶鍒跺垏鎹㈢殑鏃舵満,涓嶅啀闇�瑕侀櫡鍏ョ郴缁熺殑鍐呮牳鎬�.
Python閲屾渶甯歌鐨剏ield灏辨槸鍗忕▼鐨勬�濇兂!鍙互鏌ョ湅绗節涓棶棰�.
20 闂寘
闂寘(closure)鏄嚱鏁板紡缂栫▼鐨勯噸瑕佺殑璇硶缁撴瀯銆傞棴鍖呬篃鏄竴绉嶇粍缁囦唬鐮佺殑缁撴瀯锛屽畠鍚屾牱鎻愰珮浜嗕唬鐮佺殑鍙噸澶嶄娇鐢ㄦ�с��
褰撲竴涓唴宓屽嚱鏁板紩鐢ㄥ叾澶栭儴浣滀綔鐢ㄥ煙鐨勫彉閲�,鎴戜滑灏变細寰楀埌涓�涓棴鍖�. 鎬荤粨涓�涓�,鍒涘缓涓�涓棴鍖呭繀椤绘弧瓒充互涓嬪嚑鐐�:
[if !supportLists]1.聽[endif]蹇呴』鏈変竴涓唴宓屽嚱鏁�
[if !supportLists]2.聽[endif]鍐呭祵鍑芥暟蹇呴』寮曠敤澶栭儴鍑芥暟涓殑鍙橀噺
[if !supportLists]3.聽[endif]澶栭儴鍑芥暟鐨勮繑鍥炲�煎繀椤绘槸鍐呭祵鍑芥暟
鎰熻闂寘杩樻槸鏈夐毦搴︾殑,鍑犲彞璇濇槸璇翠笉鏄庣櫧鐨�,杩樻槸鏌ユ煡鐩稿叧璧勬枡.
閲嶇偣鏄嚱鏁拌繍琛屽悗骞朵笉浼氳鎾ら攢,灏卞儚16棰樼殑instance瀛楀吀涓�鏍�,褰撳嚱鏁拌繍琛屽畬鍚�,instance骞朵笉琚攢姣�,鑰屾槸缁х画鐣欏湪鍐呭瓨绌洪棿閲�.杩欎釜鍔熻兘绫讳技绫婚噷鐨勭被鍙橀噺,鍙笉杩囪縼绉诲埌浜嗗嚱鏁颁笂.
闂寘灏卞儚涓┖蹇冪悆涓�鏍�,浣犵煡閬撳闈㈠拰閲岄潰,浣嗕綘涓嶇煡閬撲腑闂存槸浠�涔堟牱.
21 lambda鍑芥暟
鍏跺疄灏辨槸涓�涓尶鍚嶅嚱鏁�,涓轰粈涔堝彨lambda?鍥犱负鍜屽悗闈㈢殑鍑芥暟寮忕紪绋嬫湁鍏�.
22 Python鍑芥暟寮忕紪绋�
杩欎釜闇�瑕侀�傚綋鐨勪簡瑙d竴涓嬪惂,姣曠珶鍑芥暟寮忕紪绋嬪湪Python涓篃鍋氫簡寮曠敤.
python涓嚱鏁板紡缂栫▼鏀寔:
filter 鍑芥暟鐨勫姛鑳界浉褰撲簬杩囨护鍣ㄣ�傝皟鐢ㄤ竴涓竷灏斿嚱鏁癰ool_func鏉ヨ凯浠i亶鍘嗘瘡涓猻eq涓殑鍏冪礌锛涜繑鍥炰竴涓娇bool_seq杩斿洖鍊间负true鐨勫厓绱犵殑搴忓垪銆�
聽>>>a聽=聽[1,2,3,4,5,6,7]
>>>b聽=聽filter(lambda聽x:聽x聽>聽5,聽a)
>>>print聽b
>>>[6,7]
map鍑芥暟鏄涓�涓簭鍒楃殑姣忎釜椤逛緷娆℃墽琛屽嚱鏁帮紝涓嬮潰鏄涓�涓簭鍒楁瘡涓」閮戒箻浠�2锛�
聽>>>聽a聽=聽map(lambda聽x:x*2,[1,2,3])
>>>聽list(a)
[2,聽4,聽6]
reduce鍑芥暟鏄涓�涓簭鍒楃殑姣忎釜椤硅凯浠h皟鐢ㄥ嚱鏁帮紝涓嬮潰鏄眰3鐨勯樁涔橈細
聽>>>聽reduce(lambda聽x,y:x*y,range(1,4))
6
23 Python閲岀殑鎷疯礉
寮曠敤鍜宑opy(),deepcopy()鐨勫尯鍒�
import聽copy
a聽=聽[1,聽2,聽3,聽4,聽['a',聽'b']]聽聽#鍘熷瀵硅薄
b聽=聽a聽聽#璧嬪�硷紝浼犲璞$殑寮曠敤
c聽=聽copy.copy(a)聽聽#瀵硅薄鎷疯礉锛屾祬鎷疯礉
d聽=聽copy.deepcopy(a)聽聽#瀵硅薄鎷疯礉锛屾繁鎷疯礉
a.append(5)聽聽#淇敼瀵硅薄a
a[4].append('c')聽聽#淇敼瀵硅薄a涓殑['a', 'b']鏁扮粍瀵硅薄
print聽'a = ',聽a
print聽'b = ',聽b
print聽'c = ',聽c
print聽'd = ',聽d
杈撳嚭缁撴灉锛�
a聽=聽聽[1,聽2,聽3,聽4,聽['a',聽'b',聽'c'],聽5]
b聽=聽聽[1,聽2,聽3,聽4,聽['a',聽'b',聽'c'],聽5]
c聽=聽聽[1,聽2,聽3,聽4,聽['a',聽'b',聽'c']]
d聽=聽聽[1,聽2,聽3,聽4,聽['a',聽'b']]
24 Python鍨冨溇鍥炴敹鏈哄埗
Python GC涓昏浣跨敤寮曠敤璁℃暟锛坮eference counting锛夋潵璺熻釜鍜屽洖鏀跺瀮鍦俱�傚湪寮曠敤璁℃暟鐨勫熀纭�涓婏紝閫氳繃鈥滄爣璁�-娓呴櫎鈥濓紙mark and sweep锛夎В鍐冲鍣ㄥ璞″彲鑳戒骇鐢熺殑寰幆寮曠敤闂锛岄�氳繃鈥滃垎浠e洖鏀垛�濓紙generation collection锛変互绌洪棿鎹㈡椂闂寸殑鏂规硶鎻愰珮鍨冨溇鍥炴敹鏁堢巼銆�
1 寮曠敤璁℃暟
PyObject鏄瘡涓璞″繀鏈夌殑鍐呭锛屽叾涓璷b_refcnt灏辨槸鍋氫负寮曠敤璁℃暟銆傚綋涓�涓璞℃湁鏂扮殑寮曠敤鏃讹紝瀹冪殑ob_refcnt灏变細澧炲姞锛屽綋寮曠敤瀹冪殑瀵硅薄琚垹闄わ紝瀹冪殑ob_refcnt灏变細鍑忓皯.寮曠敤璁℃暟涓�0鏃讹紝璇ュ璞$敓鍛藉氨缁撴潫浜嗐��
浼樼偣:
[if !supportLists]1.聽[endif]绠�鍗�
[if !supportLists]2.聽[endif]瀹炴椂鎬�
缂虹偣:
[if !supportLists]1.聽[endif]缁存姢寮曠敤璁℃暟娑堣�楄祫婧�
[if !supportLists]2.聽[endif]寰幆寮曠敤
2 鏍囪-娓呴櫎鏈哄埗
鍩烘湰鎬濊矾鏄厛鎸夐渶鍒嗛厤锛岀瓑鍒版病鏈夌┖闂插唴瀛樼殑鏃跺�欎粠瀵勫瓨鍣ㄥ拰绋嬪簭鏍堜笂鐨勫紩鐢ㄥ嚭鍙戯紝閬嶅巻浠ュ璞′负鑺傜偣銆佷互寮曠敤涓鸿竟鏋勬垚鐨勫浘锛屾妸鎵�鏈夊彲浠ヨ闂埌鐨勫璞℃墦涓婃爣璁帮紝鐒跺悗娓呮壂涓�閬嶅唴瀛樼┖闂达紝鎶婃墍鏈夋病鏍囪鐨勫璞¢噴鏀俱��
3 鍒嗕唬鎶�鏈�
鍒嗕唬鍥炴敹鐨勬暣浣撴�濇兂鏄細灏嗙郴缁熶腑鐨勬墍鏈夊唴瀛樺潡鏍规嵁鍏跺瓨娲绘椂闂村垝鍒嗕负涓嶅悓鐨勯泦鍚堬紝姣忎釜闆嗗悎灏辨垚涓轰竴涓�滀唬鈥濓紝鍨冨溇鏀堕泦棰戠巼闅忕潃鈥滀唬鈥濈殑瀛樻椿鏃堕棿鐨勫澶ц�屽噺灏忥紝瀛樻椿鏃堕棿閫氬父鍒╃敤缁忚繃鍑犳鍨冨溇鍥炴敹鏉ュ害閲忋��
Python榛樿瀹氫箟浜嗕笁浠e璞¢泦鍚堬紝绱㈠紩鏁拌秺澶э紝瀵硅薄瀛樻椿鏃堕棿瓒婇暱銆�
涓句緥锛氬綋鏌愪簺鍐呭瓨鍧桵缁忚繃浜�3娆″瀮鍦炬敹闆嗙殑娓呮礂涔嬪悗杩樺瓨娲绘椂锛屾垜浠氨灏嗗唴瀛樺潡M鍒掑埌涓�涓泦鍚圓涓幓锛岃�屾柊鍒嗛厤鐨勫唴瀛橀兘鍒掑垎鍒伴泦鍚圔涓幓銆傚綋鍨冨溇鏀堕泦寮�濮嬪伐浣滄椂锛屽ぇ澶氭暟鎯呭喌閮藉彧瀵归泦鍚圔杩涜鍨冨溇鍥炴敹锛岃�屽闆嗗悎A杩涜鍨冨溇鍥炴敹瑕侀殧鐩稿綋闀夸竴娈垫椂闂村悗鎵嶈繘琛岋紝杩欏氨浣垮緱鍨冨溇鏀堕泦鏈哄埗闇�瑕佸鐞嗙殑鍐呭瓨灏戜簡锛屾晥鐜囪嚜鐒跺氨鎻愰珮浜嗐�傚湪杩欎釜杩囩▼涓紝闆嗗悎B涓殑鏌愪簺鍐呭瓨鍧楃敱浜庡瓨娲绘椂闂撮暱鑰屼細琚浆绉诲埌闆嗗悎A涓紝褰撶劧锛岄泦鍚圓涓疄闄呬笂涔熷瓨鍦ㄤ竴浜涘瀮鍦撅紝杩欎簺鍨冨溇鐨勫洖鏀朵細鍥犱负杩欑鍒嗕唬鐨勬満鍒惰�岃寤惰繜銆�
25 Python閲岄潰濡備綍瀹炵幇tuple鍜宭ist鐨勮浆鎹紵
绛旓細tuple锛屽彲浠ヨ鏄笉鍙彉鐨刲ist锛岃闂柟寮忚繕鏄�氳繃绱㈠紩涓嬫爣鐨勬柟寮忋�傚綋浣犳槑纭畾涔変釜tuple鏄紝濡傛灉浠呮湁涓�涓厓绱狅紝蹇呴』甯︽湁,渚嬪锛�(1,)銆傚綋鐒讹紝鍦�2.7浠ュ悗鐨勭増锛宲ython閲岃繕澧炲姞浜嗗懡鍚嶅紡鐨則uple锛佽嚦浜庢湁浠�涔堢敤锛岄鍏堢涓�鐐癸紝妤间富鐜╄繃python閮界煡閬擄紝python鐨勫嚱鏁板彲浠ユ湁澶氳繑鍥炲�肩殑锛岃�宲ython閲岋紝澶氳繑鍥炲�硷紝灏辨槸鐢╰uple鏉ヨ〃绀猴紝杩欐槸鐢ㄧ殑鏈�骞跨殑浜嗭紝姣斿璇达紝浣犻渶瑕佸畾涔変竴涓父閲忕殑鍒楄〃锛屼絾浣犲張涓嶆兂浣跨敤list锛岄偅涔熷彲浠ユ槸瑕佷綘绠uple锛屼緥濡傦細if a in ('A','B','C'):pass
26 Python鐨刬s
is鏄姣斿湴鍧�,==鏄姣斿��
27 read,readline鍜宺eadlines
[if !supportLists]路聽[endif]read 璇诲彇鏁翠釜鏂囦欢
[if !supportLists]路聽[endif]readline 璇诲彇涓嬩竴琛�,浣跨敤鐢熸垚鍣ㄦ柟娉�
[if !supportLists]路聽[endif]readlines 璇诲彇鏁翠釜鏂囦欢鍒颁竴涓凯浠e櫒浠ヤ緵鎴戜滑閬嶅巻
28 Python2鍜�3鐨勫尯鍒�
澶ч儴鍒哖ython搴撻兘鍚屾椂鏀寔Python 2.7.x鍜�3.x鐗堟湰鐨勶紝鎵�浠ヤ笉璁洪�夋嫨鍝釜鐗堟湰閮芥槸鍙互鐨勩�備絾涓轰簡鍦ㄤ娇鐢≒ython鏃堕伩寮�鏌愪簺鐗堟湰涓竴浜涘父瑙佺殑闄烽槺锛屾垨闇�瑕佺Щ妞嶆煇涓狿ython椤圭洰
浣跨敤__future__妯″潡
print鍑芥暟
鏁存暟闄ゆ硶
Unicode
xrange
瑙﹀彂寮傚父
澶勭悊寮傚父
next()鍑芥暟鍜�.next()鏂规硶
For寰幆鍙橀噺涓庡叏灞�鍛藉悕绌洪棿娉勬紡
姣旇緝鏃犲簭绫诲瀷
浣跨敤input()瑙f瀽杈撳叆鍐呭
杩斿洖鍙凯浠e璞★紝鑰屼笉鏄垪琛�
鎺ㄨ崘锛氥��Python 2.7.x 鍜� 3.x 鐗堟湰鐨勯噸瑕佸尯鍒�銆�
29鍒板簳浠�涔堟槸Python锛熶綘鍙互鍦ㄥ洖绛斾腑涓庡叾浠栨妧鏈繘琛屽姣�
绛旀
涓嬮潰鏄竴浜涘叧閿偣锛�
[if !supportLists]路聽[endif]Python鏄竴绉嶈В閲婂瀷璇█銆傝繖灏辨槸璇达紝涓嶤璇█鍜孋鐨勮鐢熻瑷�涓嶅悓锛孭ython浠g爜鍦ㄨ繍琛屼箣鍓嶄笉闇�瑕佺紪璇戙�傚叾浠栬В閲婂瀷璇█杩樺寘鎷琍HP鍜孯uby銆�
[if !supportLists]路聽[endif]Python鏄姩鎬佺被鍨嬭瑷�锛屾寚鐨勬槸浣犲湪澹版槑鍙橀噺鏃讹紝涓嶉渶瑕佽鏄庡彉閲忕殑绫诲瀷銆備綘鍙互鐩存帴缂栧啓绫讳技x=111鍜寈="I'm a string"杩欐牱鐨勪唬鐮侊紝绋嬪簭涓嶄細鎶ラ敊銆�
[if !supportLists]路聽[endif]Python闈炲父閫傚悎闈㈠悜瀵硅薄鐨勭紪绋嬶紙OOP锛夛紝鍥犱负瀹冩敮鎸侀�氳繃缁勫悎锛坈omposition锛変笌缁ф壙锛坕nheritance锛夌殑鏂瑰紡瀹氫箟绫伙紙class锛夈�侾ython涓病鏈夎闂鏄庣锛坅ccess specifier锛岀被浼糃++涓殑public鍜宲rivate锛夛紝杩欎箞璁捐鐨勪緷鎹槸鈥滃ぇ瀹堕兘鏄垚骞翠汉浜嗏�濄��
[if !supportLists]路聽[endif]鍦≒ython璇█涓紝鍑芥暟鏄涓�绫诲璞★紙first-class objects锛夈�傝繖鎸囩殑鏄畠浠彲浠ヨ鎸囧畾缁欏彉閲忥紝鍑芥暟鏃㈣兘杩斿洖鍑芥暟绫诲瀷锛屼篃鍙互鎺ュ彈鍑芥暟浣滀负杈撳叆銆傜被锛坈lass锛変篃鏄涓�绫诲璞°��
[if !supportLists]路聽[endif]Python浠g爜缂栧啓蹇紝浣嗘槸杩愯閫熷害姣旂紪璇戣瑷�閫氬父瑕佹參銆傚ソ鍦≒ython鍏佽鍔犲叆鍩轰簬C璇█缂栧啓鐨勬墿灞曪紝鍥犳鎴戜滑鑳藉浼樺寲浠g爜锛屾秷闄ょ摱棰堬紝杩欑偣閫氬父鏄彲浠ュ疄鐜扮殑銆俷umpy灏辨槸涓�涓緢濂藉湴渚嬪瓙锛屽畠鐨勮繍琛岄�熷害鐪熺殑闈炲父蹇紝鍥犱负寰堝绠楁湳杩愮畻鍏跺疄骞朵笉鏄�氳繃Python瀹炵幇鐨勩��
[if !supportLists]路聽[endif]Python鐢ㄩ�旈潪甯稿箍娉涒�斺�旂綉缁滃簲鐢紝鑷姩鍖栵紝绉戝寤烘ā锛屽ぇ鏁版嵁搴旂敤锛岀瓑绛夈�傚畠涔熷父琚敤浣溾�滆兌姘磋瑷�鈥濓紝甯姪鍏朵粬璇█鍜岀粍浠舵敼鍠勮繍琛岀姸鍐点��
[if !supportLists]路聽[endif]Python璁╁洶闅剧殑浜嬫儏鍙樺緱瀹规槗锛屽洜姝ょ▼搴忓憳鍙互涓撴敞浜庣畻娉曞拰鏁版嵁缁撴瀯鐨勮璁★紝鑰屼笉鐢ㄥ鐞嗗簳灞傜殑缁嗚妭銆�
涓轰粈涔堟彁杩欎釜闂锛�
濡傛灉浣犲簲鑱樼殑鏄竴涓狿ython寮�鍙戝矖浣嶏紝浣犲氨搴旇鐭ラ亾杩欐槸闂ㄤ粈涔堟牱鐨勮瑷�锛屼互鍙婂畠涓轰粈涔堣繖涔堥叿銆備互鍙婂畠鍝噷涓嶅ソ銆�
30琛ュ厖缂哄け鐨勪唬鐮�
def print_directory_contents(sPath):
聽聽聽聽"""
杩欎釜鍑芥暟鎺ュ彈鏂囦欢澶圭殑鍚嶇О浣滀负杈撳叆鍙傛暟锛�
杩斿洖璇ユ枃浠跺す涓枃浠剁殑璺緞锛�
浠ュ強鍏跺寘鍚枃浠跺す涓枃浠剁殑璺緞銆�
聽聽聽聽"""
聽聽聽聽# 琛ュ厖浠g爜
绛旀
def print_directory_contents(sPath):
聽聽聽聽import os 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽for sChild in os.listdir(sPath): 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽sChildPath = os.path.join(sPath,sChild)
聽聽聽聽聽聽聽聽if os.path.isdir(sChildPath):
聽聽聽聽聽聽聽聽聽聽聽聽print_directory_contents(sChildPath)
聽聽聽聽聽聽聽聽else:
聽聽聽聽聽聽聽聽聽聽聽聽print sChildPath
鐗瑰埆瑕佹敞鎰忎互涓嬪嚑鐐癸細
[if !supportLists]路聽[endif]鍛藉悕瑙勮寖瑕佺粺涓�銆傚鏋滄牱鏈唬鐮佷腑鑳藉鐪嬪嚭鍛藉悕瑙勮寖锛岄伒寰叾宸叉湁鐨勮鑼冦��
[if !supportLists]路聽[endif]閫掑綊鍑芥暟闇�瑕侀�掑綊骞剁粓姝€�傜‘淇濅綘鏄庣櫧鍏朵腑鐨勫師鐞嗭紝鍚﹀垯浣犲皢闈复鏃犱紤鏃犳鐨勮皟鐢ㄦ爤锛坈allstack锛夈��
[if !supportLists]路聽[endif]鎴戜滑浣跨敤os妯″潡涓庢搷浣滅郴缁熻繘琛屼氦浜掞紝鍚屾椂鍋氬埌浜や簰鏂瑰紡鏄彲浠ヨ法骞冲彴鐨勩�備綘鍙互鎶婁唬鐮佸啓鎴恠ChildPath = sPath + '/' + sChild锛屼絾鏄繖涓湪Windows绯荤粺涓婁細鍑洪敊銆�
[if !supportLists]路聽[endif]鐔熸倝鍩虹妯″潡鏄潪甯告湁浠峰�肩殑锛屼絾鏄埆鎯崇牬鑴戣閮借儗涓嬫潵锛岃浣廏oogle鏄綘宸ヤ綔涓殑鑹笀鐩婂弸銆�
[if !supportLists]路聽[endif]濡傛灉浣犱笉鏄庣櫧浠g爜鐨勯鏈熷姛鑳斤紝灏卞ぇ鑳嗘彁闂��
[if !supportLists]路聽[endif]鍧氭寔KISS鍘熷垯锛佷繚鎸佺畝鍗曪紝涓嶈繃鑴戝瓙灏辫兘鎳傦紒
涓轰粈涔堟彁杩欎釜闂锛�
[if !supportLists]路聽[endif]璇存槑闈㈣瘯鑰呭涓庢搷浣滅郴缁熶氦浜掔殑鍩虹鐭ヨ瘑
[if !supportLists]路聽[endif]閫掑綊鐪熸槸澶ソ鐢ㄥ暒
31闃呰涓嬮潰鐨勪唬鐮侊紝鍐欏嚭A0锛孉1鑷矨n鐨勬渶缁堝�笺��
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = [i for i in A1 if i in A0]
A3 = [A0[s] for s in A0]
A4 = [i for i in A1 if i in A3]
A5 = {i:i*i for i in A1}
A6 = [[i,i*i] for i in A1]
绛旀
A0 = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
A1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A2 = []
A3 = [1, 3, 2, 5, 4]
A4 = [1, 2, 3, 4, 5]
A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
涓轰粈涔堟彁杩欎釜闂锛�
[if !supportLists]路聽[endif]鍒楄〃瑙f瀽锛坙ist comprehension锛夊崄鍒嗚妭绾︽椂闂达紝瀵瑰緢澶氫汉鏉ヨ涔熸槸涓�涓ぇ鐨勫涔犻殰纰嶃��
[if !supportLists]路聽[endif]濡傛灉浣犺鎳備簡杩欎簺浠g爜锛屽氨寰堝彲鑳藉彲浠ュ啓涓嬫纭湴鍊笺��
[if !supportLists]路聽[endif]鍏朵腑閮ㄥ垎浠g爜鏁呮剰鍐欑殑鎬�殑銆傚洜涓轰綘鍏变簨鐨勪汉涔嬩腑涔熶細鏈夋�汉銆�
32涓嬮潰浠g爜浼氳緭鍑轰粈涔堬細
def f(x,l=[]):
聽聽聽聽for i in range(x):
聽聽聽聽聽聽聽聽l.append(i*i)
聽聽聽聽print(l)
f(2)
f(3,[3,2,1])
f(3)
绛旀锛�
[0, 1]
[3, 2, 1, 0, 1, 4]
[0, 1, 0, 1, 4]
鍛冿紵
绗竴涓嚱鏁拌皟鐢ㄥ崄鍒嗘槑鏄撅紝for寰幆鍏堝悗灏�0鍜�1娣诲姞鑷充簡绌哄垪琛╨涓�俵鏄彉閲忕殑鍚嶅瓧锛屾寚鍚戝唴瀛樹腑瀛樺偍鐨勪竴涓垪琛ㄣ�傜浜屼釜鍑芥暟璋冪敤鍦ㄤ竴鍧楁柊鐨勫唴瀛樹腑鍒涘缓浜嗘柊鐨勫垪琛ㄣ�俵杩欐椂鎸囧悜浜嗘柊鐢熸垚鐨勫垪琛ㄣ�備箣鍚庡啀寰�鏂板垪琛ㄤ腑娣诲姞0銆�1鍜�4銆傚緢妫掑惂銆傜涓変釜鍑芥暟璋冪敤鐨勭粨鏋滃氨鏈変簺濂囨�簡銆傚畠浣跨敤浜嗕箣鍓嶅唴瀛樺湴鍧�涓瓨鍌ㄧ殑鏃у垪琛ㄣ�傝繖灏辨槸涓轰粈涔堝畠鐨勫墠涓や釜鍏冪礌鏄�0鍜�1浜嗐��
33浣犲浣曠鐞嗕笉鍚岀増鏈殑浠g爜锛�
绛旀锛�
鐗堟湰绠$悊锛佽闂埌杩欎釜闂鐨勬椂鍊欙紝浣犲簲璇ヨ琛ㄧ幇寰楀緢鍏村锛岀敋鑷冲憡璇変粬浠綘鏄浣曚娇鐢℅it锛堟垨鏄叾浠栦綘鏈�鍠滄鐨勫伐鍏凤級杩借釜鑷繁鍜屽ザ濂剁殑涔︿俊寰�鏉ャ�傛垜鍋忓悜浜庝娇鐢℅it浣滀负鐗堟湰鎺у埗绯荤粺锛圴CS锛夛紝浣嗚繕鏈夊叾浠栫殑閫夋嫨锛屾瘮濡俿ubversion锛圫VN锛夈��
涓轰粈涔堟彁杩欎釜闂锛�
鍥犱负娌℃湁鐗堟湰鎺у埗鐨勪唬鐮侊紝灏卞儚娌℃湁鏉瓙鐨勫挅鍟°�傛湁鏃跺�欐垜浠渶瑕佸啓涓�浜涗竴娆℃�х殑銆佸彲浠ラ殢鎵嬫墧鎺夌殑鑴氭湰锛岃繖绉嶆儏鍐典笅涓嶄綔鐗堟湰鎺у埗娌″叧绯汇�備絾鏄鏋滀綘闈㈠鐨勬槸澶ч噺鐨勪唬鐮侊紝浣跨敤鐗堟湰鎺у埗绯荤粺鏄湁鍒╃殑銆傜増鏈帶鍒惰兘澶熷府浣犺拷韪皝瀵逛唬鐮佸簱鍋氫簡浠�涔堟搷浣滐紱鍙戠幇鏂板紩鍏ヤ簡浠�涔坆ug锛涚鐞嗕綘鐨勮蒋浠剁殑涓嶅悓鐗堟湰鍜屽彂琛岀増锛涘湪鍥㈤槦鎴愬憳涓垎浜簮浠g爜锛涢儴缃插強鍏朵粬鑷姩鍖栧鐞嗐�傚畠鑳借浣犲洖婊氬埌鍑虹幇闂涔嬪墠鐨勭増鏈紝鍗曞嚟杩欑偣灏辩壒鍒浜嗐�傝繕鏈夊叾浠栫殑濂藉姛鑳姐�傛�庝箞涓�涓瀛椾簡寰楋紒
34鈥滅尨瀛愯ˉ涓佲�濓紙monkey patching锛夋寚鐨勬槸浠�涔堬紵杩欑鍋氭硶濂藉悧锛�
绛旀锛�
鈥滅尨瀛愯ˉ涓佲�濆氨鏄寚锛屽湪鍑芥暟鎴栧璞″凡缁忓畾涔変箣鍚庯紝鍐嶅幓鏀瑰彉瀹冧滑鐨勮涓恒��
涓句釜渚嬪瓙锛�
import datetime
datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12)
澶ч儴鍒嗘儏鍐典笅锛岃繖鏄寰堜笉濂界殑鍋氭硶- 鍥犱负鍑芥暟鍦ㄤ唬鐮佸簱涓殑琛屼负鏈�濂芥槸閮戒繚鎸佷竴鑷淬�傛墦鈥滅尨瀛愯ˉ涓佲�濈殑鍘熷洜鍙兘鏄负浜嗘祴璇曘�俶ock鍖呭瀹炵幇杩欎釜鐩殑寰堟湁甯姪銆�
涓轰粈涔堟彁杩欎釜闂锛�
绛斿杩欎釜闂璇存槑浣犲鍗曞厓娴嬭瘯鐨勬柟娉曟湁涓�瀹氫簡瑙c�備綘濡傛灉鎻愬埌瑕侀伩鍏嶁�滅尨瀛愯ˉ涓佲�濓紝鍙互璇存槑浣犱笉鏄偅绉嶅枩娆㈣姳閲岃儭鍝ㄤ唬鐮佺殑绋嬪簭鍛橈紙鍏徃閲屽氨鏈夎繖绉嶄汉锛岃窡浠栦滑鍏变簨鐪熸槸绯熺硶閫忎簡锛夛紝鑰屾槸鏇存敞閲嶅彲缁存姢鎬с�傝繕璁板緱KISS鍘熷垯鐮侊紵绛斿杩欎釜闂杩樿鏄庝綘鏄庣櫧涓�浜汸ython搴曞眰杩愪綔鐨勬柟寮忥紝鍑芥暟瀹為檯鏄浣曞瓨鍌ㄣ�佽皟鐢ㄧ瓑绛夈��
鍙﹀锛氬鏋滀綘娌¤杩噈ock妯″潡鐨勮瘽锛岀湡鐨勫�煎緱鑺辨椂闂磋涓�璇汇�傝繖涓ā鍧楅潪甯告湁鐢ㄣ��
35闃呰涓嬮潰鐨勪唬鐮侊紝瀹冪殑杈撳嚭缁撴灉鏄粈涔堬紵
class A(object):
聽聽聽聽def go(self):
聽聽聽聽聽聽聽聽print "go A go!"
聽聽聽聽def stop(self):
聽聽聽聽聽聽聽聽print "stop A stop!"
聽聽聽聽def pause(self):
聽聽聽聽聽聽聽聽raise Exception("Not Implemented")
class B(A):
聽聽聽聽def go(self):
聽聽聽聽聽聽聽聽super(B, self).go()
聽聽聽聽聽聽聽聽print "go B go!"
class C(A):
聽聽聽聽def go(self):
聽聽聽聽聽聽聽聽super(C, self).go()
聽聽聽聽聽聽聽聽print "go C go!"
聽聽聽聽def stop(self):
聽聽聽聽聽聽聽聽super(C, self).stop()
聽聽聽聽聽聽聽聽print "stop C stop!"
class D(B,C):
聽聽聽聽def go(self):
聽聽聽聽聽聽聽聽super(D, self).go()
聽聽聽聽聽聽聽聽print "go D go!"
聽聽聽聽def stop(self):
聽聽聽聽聽聽聽聽super(D, self).stop()
聽聽聽聽聽聽聽聽print "stop D stop!"
聽聽聽聽def pause(self):
聽聽聽聽聽聽聽聽print "wait D wait!"
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
# 璇存槑涓嬪垪浠g爜鐨勮緭鍑虹粨鏋�
a.go()
b.go()
c.go()
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()
绛旀
杈撳嚭缁撴灉浠ユ敞閲婄殑褰㈠紡琛ㄧず锛�
a.go()
# go A go!
b.go()
# go A go!
# go B go!
c.go()
# go A go!
# go C go!
d.go()
# go A go!
# go C go!
# go B go!
# go D go!
e.go()
# go A go!
# go C go!
# go B go!
a.stop()
# stop A stop!
b.stop()
# stop A stop!
c.stop()
# stop A stop!
# stop C stop!
d.stop()
# stop A stop!
# stop C stop!
# stop D stop!
e.stop()
# stop A stop!
a.pause()
# ... Exception: Not Implemented
b.pause()
# ... Exception: Not Implemented
c.pause()
# ... Exception: Not Implemented
d.pause()
# wait D wait!
e.pause()
# ...Exception: Not Implemented
涓轰粈涔堟彁杩欎釜闂锛�
鍥犱负闈㈠悜瀵硅薄鐨勭紪绋嬬湡鐨勭湡鐨勫緢閲嶈銆備笉楠椾綘銆傜瓟瀵硅繖閬撻棶棰樿鏄庝綘鐞嗚В浜嗙户鎵垮拰Python涓璼uper鍑芥暟鐨勭敤娉曘��
36闃呰涓嬮潰鐨勪唬鐮侊紝瀹冪殑杈撳嚭缁撴灉鏄粈涔堬紵
class Node(object):
聽聽聽聽def __init__(self,sName):
聽聽聽聽聽聽聽聽self._lChildren = []
聽聽聽聽聽聽聽聽self.sName = sName
聽聽聽聽def __repr__(self):
聽聽聽聽聽聽聽聽return "".format(self.sName)
聽聽聽聽def append(self,*args,**kwargs):
聽聽聽聽聽聽聽聽self._lChildren.append(*args,**kwargs)
聽聽聽聽def print_all_1(self):
聽聽聽聽聽聽聽聽print self
聽聽聽聽聽聽聽聽for oChild in self._lChildren:
聽聽聽聽聽聽聽聽聽聽聽聽oChild.print_all_1()
聽聽聽聽def print_all_2(self):
聽聽聽聽聽聽聽聽def gen(o):
聽聽聽聽聽聽聽聽聽聽聽聽lAll = [o,]
聽聽聽聽聽聽聽聽聽聽聽聽while lAll:
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽oNext = lAll.pop(0)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽lAll.extend(oNext._lChildren)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽yield oNext
聽聽聽聽聽聽聽聽for oNode in gen(self):
聽聽聽聽聽聽聽聽聽聽聽聽print oNode
oRoot = Node("root")
oChild1 = Node("child1")
oChild2 = Node("child2")
oChild3 = Node("child3")
oChild4 = Node("child4")
oChild5 = Node("child5")
oChild6 = Node("child6")
oChild7 = Node("child7")
oChild8 = Node("child8")
oChild9 = Node("child9")
oChild10 = Node("child10")
oRoot.append(oChild1)
oRoot.append(oChild2)
oRoot.append(oChild3)
oChild1.append(oChild4)
oChild1.append(oChild5)
oChild2.append(oChild6)
oChild4.append(oChild7)
oChild3.append(oChild8)
oChild3.append(oChild9)
oChild6.append(oChild10)
# 璇存槑涓嬮潰浠g爜鐨勮緭鍑虹粨鏋�
oRoot.print_all_1()
oRoot.print_all_2()
绛旀
oRoot.print_all_1()浼氭墦鍗颁笅闈㈢殑缁撴灉锛�
oRoot.print_all_1()浼氭墦鍗颁笅闈㈢殑缁撴灉锛�
涓轰粈涔堟彁杩欎釜闂锛�
鍥犱负瀵硅薄鐨勭簿楂撳氨鍦ㄤ簬缁勫悎锛坈omposition锛変笌瀵硅薄鏋勯�狅紙object construction锛夈�傚璞¢渶瑕佹湁缁勫悎鎴愬垎鏋勬垚锛岃�屼笖寰椾互鏌愮鏂瑰紡鍒濆鍖栥�傝繖閲屼篃娑夊強鍒伴�掑綊鍜岀敓鎴愬櫒锛坓enerator锛夌殑浣跨敤銆�
鐢熸垚鍣ㄦ槸寰堟鐨勬暟鎹被鍨嬨�備綘鍙互鍙�氳繃鏋勯�犱竴涓緢闀跨殑鍒楄〃锛岀劧鍚庢墦鍗板垪琛ㄧ殑鍐呭锛屽氨鍙互鍙栧緱涓巔rint_all_2绫讳技鐨勫姛鑳姐�傜敓鎴愬櫒杩樻湁涓�涓ソ澶勶紝灏辨槸涓嶇敤鍗犳嵁寰堝鍐呭瓨銆�
鏈変竴鐐硅繕鍊煎緱鎸囧嚭锛屽氨鏄痯rint_all_1浼氫互娣卞害浼樺厛锛坉epth-first锛夌殑鏂瑰紡閬嶅巻鏍�(tree),鑰宲rint_all_2鍒欐槸瀹藉害浼樺厛锛坵idth-first锛夈�傛湁鏃跺�欙紝涓�绉嶉亶鍘嗘柟寮忔瘮鍙︿竴绉嶆洿鍚堥�傘�備絾杩欒鐪嬩綘鐨勫簲鐢ㄧ殑鍏蜂綋鎯呭喌銆�
36.浠嬬粛涓�涓媏xcept鐨勭敤娉曞拰浣滅敤锛�
绛旓細try鈥xcept鈥xcept鈥else鈥[finally鈥
鎵цtry涓嬬殑璇彞锛屽鏋滃紩鍙戝紓甯革紝鍒欐墽琛岃繃绋嬩細璺冲埌except璇彞銆傚姣忎釜except鍒嗘敮椤哄簭灏濊瘯鎵ц锛屽鏋滃紩鍙戠殑寮傚父涓巈xcept涓殑寮傚父缁勫尮閰嶏紝鎵ц鐩稿簲鐨勮鍙ャ�傚鏋滄墍鏈夌殑except閮戒笉鍖归厤锛屽垯寮傚父浼氫紶閫掑埌涓嬩竴涓皟鐢ㄦ湰浠g爜鐨勬渶楂樺眰try浠g爜涓��
try涓嬬殑璇彞姝e父鎵ц锛屽垯鎵цelse鍧椾唬鐮併�傚鏋滃彂鐢熷紓甯革紝灏变笉浼氭墽琛�
濡傛灉瀛樺湪finally璇彞锛屾渶鍚庢�绘槸浼氭墽琛屻��
37.Python涓璸ass璇彞鐨勪綔鐢ㄦ槸浠�涔堬紵
绛旓細pass璇彞涓嶄細鎵ц浠讳綍鎿嶄綔锛屼竴鑸綔涓哄崰浣嶇鎴栬�呭垱寤哄崰浣嶇▼搴忥紝whileFalse:pass
38.浠嬬粛涓�涓婸ython涓媟ange()鍑芥暟鐨勭敤娉曪紵
绛旓細鍒楀嚭涓�缁勬暟鎹紝缁忓父鐢ㄥ湪for 聽in range()寰幆涓�
39.濡備綍鐢≒ython鏉ヨ繘琛屾煡璇㈠拰鏇挎崲涓�涓枃鏈瓧绗︿覆锛�
绛旓細鍙互浣跨敤re妯″潡涓殑sub()鍑芥暟鎴栬�卻ubn()鍑芥暟鏉ヨ繘琛屾煡璇㈠拰鏇挎崲锛�
鏍煎紡锛歴ub(replacement, string[,count=0])锛坮eplacement鏄鏇挎崲鎴愮殑鏂囨湰锛宻tring鏄渶瑕佽鏇挎崲鐨勬枃鏈紝count鏄竴涓彲閫夊弬鏁帮紝鎸囨渶澶ц鏇挎崲鐨勬暟閲忥級
>>> import re
>>>p=re.compile(鈥榖lue|white|red鈥�)
>>>print(p.sub(鈥榗olour鈥�,'blue socks and red shoes鈥�))
colour socks and colourshoes
>>>print(p.sub(鈥榗olour鈥�,'blue socks and red shoes鈥�,count=1))
colour socks and redshoes
subn()鏂规硶鎵ц鐨勬晥鏋滆窡sub()涓�鏍凤紝涓嶈繃瀹冧細杩斿洖涓�涓簩缁存暟缁勶紝鍖呮嫭鏇挎崲鍚庣殑鏂扮殑瀛楃涓插拰鎬诲叡鏇挎崲鐨勬暟閲�
40.Python閲岄潰match()鍜宻earch()鐨勫尯鍒紵
绛旓細re妯″潡涓璵atch(pattern,string[,flags]),妫�鏌tring鐨勫紑澶存槸鍚︿笌pattern鍖归厤銆�
re妯″潡涓璻esearch(pattern,string[,flags]),鍦╯tring鎼滅储pattern鐨勭涓�涓尮閰嶅�笺��
>>>print(re.match(鈥榮uper鈥�, 鈥榮uperstition鈥�).span())
(0, 5)
>>>print(re.match(鈥榮uper鈥�, 鈥榠nsuperable鈥�))
None
>>>print(re.search(鈥榮uper鈥�, 鈥榮uperstition鈥�).span())
(0, 5)
>>>print(re.search(鈥榮uper鈥�, 鈥榠nsuperable鈥�).span())
(2, 7)
41.鐢≒ython鍖归厤HTML tag鐨勬椂鍊欙紝<.*>鍜�<.*?>鏈変粈涔堝尯鍒紵
绛旓細鏈鍙椽濠尮閰�( <.*> )鍜岄潪璐┆鍖归厤(<.*?> )
渚嬪:
test
<.*> :
test
<.*?> :
42.Python閲岄潰濡備綍鐢熸垚闅忔満鏁帮紵
绛旓細random妯″潡
闅忔満鏁存暟锛歳andom.randint(a,b)锛氳繑鍥為殢鏈烘暣鏁皒,a<=x<=b
random.randrange(start,stop,[,step])锛氳繑鍥炰竴涓寖鍥村湪(start,stop,step)涔嬮棿鐨勯殢鏈烘暣鏁帮紝涓嶅寘鎷粨鏉熷�笺��
闅忔満瀹炴暟锛歳andom.random( ):杩斿洖0鍒�1涔嬮棿鐨勬诞鐐规暟
random.uniform(a,b):杩斿洖鎸囧畾鑼冨洿鍐呯殑娴偣鏁般��
43.鏈夋病鏈変竴涓伐鍏峰彲浠ュ府鍔╂煡鎵緋ython鐨刡ug鍜岃繘琛岄潤鎬佺殑浠g爜鍒嗘瀽锛�
绛旓細PyChecker鏄竴涓猵ython浠g爜鐨勯潤鎬佸垎鏋愬伐鍏凤紝瀹冨彲浠ュ府鍔╂煡鎵緋ython浠g爜鐨刡ug, 浼氬浠g爜鐨勫鏉傚害鍜屾牸寮忔彁鍑鸿鍛�
Pylint鏄彟澶栦竴涓伐鍏峰彲浠ヨ繘琛宑odingstandard妫�鏌�
44.濡備綍鍦ㄤ竴涓猣unction閲岄潰璁剧疆涓�涓叏灞�鐨勫彉閲忥紵
绛旓細瑙e喅鏂规硶鏄湪function鐨勫紑濮嬫彃鍏ヤ竴涓猤lobal澹版槑锛�
def f()
global x
45.鍗曞紩鍙凤紝鍙屽紩鍙凤紝涓夊紩鍙风殑鍖哄埆
绛旓細鍗曞紩鍙峰拰鍙屽紩鍙锋槸绛夋晥鐨勶紝濡傛灉瑕佹崲琛岋紝闇�瑕佺鍙�(\),涓夊紩鍙峰垯鍙互鐩存帴鎹㈣锛屽苟涓斿彲浠ュ寘鍚敞閲�
濡傛灉瑕佽〃绀篖et鈥檚 go 杩欎釜瀛楃涓�
鍗曞紩鍙凤細s4 = 鈥楲et\鈥檚 go鈥�
鍙屽紩鍙凤細s5 = 鈥淟et鈥檚 go鈥�
s6 = 鈥業 realy like鈥減ython鈥�!鈥�
杩欏氨鏄崟寮曞彿鍜屽弻寮曞彿閮藉彲浠ヨ〃绀哄瓧绗︿覆鐨勫師鍥犱簡
46 Python鍜屽绾跨▼锛坢ulti-threading锛夈�傝繖鏄釜濂戒富鎰忕爜锛熷垪涓句竴浜涜Python浠g爜浠ュ苟琛屾柟寮忚繍琛岀殑鏂规硶銆�
绛旀
Python骞朵笉鏀寔鐪熸鎰忎箟涓婄殑澶氱嚎绋嬨�侾ython涓彁渚涗簡澶氱嚎绋嬪寘锛屼絾鏄鏋滀綘鎯抽�氳繃澶氱嚎绋嬫彁楂樹唬鐮佺殑閫熷害锛屼娇鐢ㄥ绾跨▼鍖呭苟涓嶆槸涓ソ涓绘剰銆侾ython涓湁涓�涓绉颁负Global Interpreter Lock锛圙IL锛夌殑涓滆タ锛屽畠浼氱‘淇濅换浣曟椂鍊欎綘鐨勫涓嚎绋嬩腑锛屽彧鏈変竴涓鎵ц銆傜嚎绋嬬殑鎵ц閫熷害闈炲父涔嬪揩锛屼細璁╀綘璇互涓虹嚎绋嬫槸骞惰鎵ц鐨勶紝浣嗘槸瀹為檯涓婇兘鏄疆娴佹墽琛屻�傜粡杩嘒IL杩欎竴閬撳叧鍗″鐞嗭紝浼氬鍔犳墽琛岀殑寮�閿�銆傝繖鎰忓懗鐫�锛屽鏋滀綘鎯虫彁楂樹唬鐮佺殑杩愯閫熷害锛屼娇鐢╰hreading鍖呭苟涓嶆槸涓�涓緢濂界殑鏂规硶銆�
涓嶈繃杩樻槸鏈夊緢澶氱悊鐢变績浣挎垜浠娇鐢╰hreading鍖呯殑銆傚鏋滀綘鎯冲悓鏃舵墽琛屼竴浜涗换鍔★紝鑰屼笖涓嶈�冭檻鏁堢巼闂锛岄偅涔堜娇鐢ㄨ繖涓寘鏄畬鍏ㄦ病闂鐨勶紝鑰屼笖涔熷緢鏂逛究銆備絾鏄ぇ閮ㄥ垎鎯呭喌涓嬶紝骞朵笉鏄繖涔堜竴鍥炰簨锛屼綘浼氬笇鏈涙妸澶氱嚎绋嬬殑閮ㄥ垎澶栧寘缁欐搷浣滅郴缁熷畬鎴愶紙閫氳繃寮�鍚涓繘绋嬶級锛屾垨鑰呮槸鏌愪簺璋冪敤浣犵殑Python浠g爜鐨勫閮ㄧ▼搴忥紙渚嬪Spark鎴朒adoop锛夛紝鍙堟垨鑰呮槸浣犵殑Python浠g爜璋冪敤鐨勫叾浠栦唬鐮侊紙渚嬪锛屼綘鍙互鍦≒ython涓皟鐢–鍑芥暟锛岀敤浜庡鐞嗗紑閿�杈冨ぇ鐨勫绾跨▼宸ヤ綔锛夈��
涓轰粈涔堟彁杩欎釜闂
鍥犱负GIL灏辨槸涓贩璐︿笢瑗匡紙A-hole锛夈�傚緢澶氫汉鑺辫垂澶ч噺鐨勬椂闂达紝璇曞浘瀵绘壘鑷繁澶氱嚎绋嬩唬鐮佷腑鐨勭摱棰堬紝鐩村埌浠栦滑鏄庣櫧GIL鐨勫瓨鍦ㄣ��
47 灏嗕笅闈㈢殑鍑芥暟鎸夌収鎵ц鏁堢巼楂樹綆鎺掑簭銆�
瀹冧滑閮芥帴鍙楃敱0鑷�1涔嬮棿鐨勬暟瀛楁瀯鎴愮殑鍒楄〃浣滀负杈撳叆銆傝繖涓垪琛ㄥ彲浠ュ緢闀裤�備竴涓緭鍏ュ垪琛ㄧ殑绀轰緥濡備笅锛歔random.random() for i in range(100000)]銆備綘濡備綍璇佹槑鑷繁鐨勭瓟妗堟槸姝g‘鐨勩��
def f1(lIn):
聽聽聽聽l1 = sorted(lIn)
聽聽聽聽l2 = [i for i in l1 if i<0.5]
聽聽聽聽return [i*i for i in l2]
def f2(lIn):
聽聽聽聽l1 = [i for i in lIn if i<0.5]
聽聽聽聽l2 = sorted(l1)
聽聽聽聽return [i*i for i in l2]
def f3(lIn):
聽聽聽聽l1 = [i*i for i in lIn]
聽聽聽聽l2 = sorted(l1)
聽聽聽聽return [i for i in l1 if i<(0.5*0.5)]
绛旀
鎸夋墽琛屾晥鐜囦粠楂樺埌浣庢帓鍒楋細f2銆乫1鍜宖3銆傝璇佹槑杩欎釜绛旀鏄鐨勶紝浣犲簲璇ョ煡閬撳浣曞垎鏋愯嚜宸变唬鐮佺殑鎬ц兘銆侾ython涓湁涓�涓緢濂界殑绋嬪簭鍒嗘瀽鍖咃紝鍙互婊¤冻杩欎釜闇�姹傘��
import cProfile
lIn = [random.random() for i in range(100000)]
cProfile.run('f1(lIn)')
cProfile.run('f2(lIn)')
cProfile.run('f3(lIn)')
涓轰簡鍚戝ぇ瀹惰繘琛屽畬鏁村湴璇存槑锛屼笅闈㈡垜浠粰鍑轰笂杩板垎鏋愪唬鐮佺殑杈撳嚭缁撴灉锛�
>>> cProfile.run('f1(lIn)')
聽聽聽聽聽聽聽聽聽4 function calls in 0.045 seconds
聽聽聽Ordered by: standard name
聽聽聽ncalls 聽tottime 聽percall 聽cumtime 聽percall filename:lineno(function)
聽聽聽聽聽聽聽聽1 聽聽聽0.009 聽聽聽0.009 聽聽聽0.044 聽聽聽0.044 :1(f1)
聽聽聽聽聽聽聽聽1 聽聽聽0.001 聽聽聽0.001 聽聽聽0.045 聽聽聽0.045 :1()
聽聽聽聽聽聽聽聽1 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 {method 'disable' of '_lsprof.Profiler' objects}
聽聽聽聽聽聽聽聽1 聽聽聽0.035 聽聽聽0.035 聽聽聽0.035 聽聽聽0.035 {sorted}
>>> cProfile.run('f2(lIn)')
聽聽聽聽聽聽聽聽聽4 function calls in 0.024 seconds
聽聽聽Ordered by: standard name
聽聽聽ncalls 聽tottime 聽percall 聽cumtime 聽percall filename:lineno(function)
聽聽聽聽聽聽聽聽1 聽聽聽0.008 聽聽聽0.008 聽聽聽0.023 聽聽聽0.023 :1(f2)
聽聽聽聽聽聽聽聽1 聽聽聽0.001 聽聽聽0.001 聽聽聽0.024 聽聽聽0.024 :1()
聽聽聽聽聽聽聽聽1 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 {method 'disable' of '_lsprof.Profiler' objects}
聽聽聽聽聽聽聽聽1 聽聽聽0.016 聽聽聽0.016 聽聽聽0.016 聽聽聽0.016 {sorted}
>>> cProfile.run('f3(lIn)')
聽聽聽聽聽聽聽聽聽4 function calls in 0.055 seconds
聽聽聽Ordered by: standard name
聽聽聽ncalls 聽tottime 聽percall 聽cumtime 聽percall filename:lineno(function)
聽聽聽聽聽聽聽聽1 聽聽聽0.016 聽聽聽0.016 聽聽聽0.054 聽聽聽0.054 :1(f3)
聽聽聽聽聽聽聽聽1 聽聽聽0.001 聽聽聽0.001 聽聽聽0.055 聽聽聽0.055 :1()
聽聽聽聽聽聽聽聽1 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 聽聽聽0.000 {method 'disable' of '_lsprof.Profiler' objects}
聽聽聽聽聽聽聽聽1 聽聽聽0.038 聽聽聽0.038 聽聽聽0.038 聽聽聽0.038 {sorted}
涓轰粈涔堟彁杩欎釜闂锛�
瀹氫綅骞堕伩鍏嶄唬鐮佺摱棰堟槸闈炲父鏈変环鍊肩殑鎶�鑳姐�傛兂瑕佺紪鍐欒澶氶珮鏁堢殑浠g爜锛屾渶缁堥兘瑕佸洖绛斿父璇嗕笂鏉モ�斺�斿湪涓婇潰鐨勪緥瀛愪腑锛屽鏋滃垪琛ㄨ緝灏忕殑璇濓紝寰堟槑鏄炬槸鍏堣繘琛屾帓搴忔洿蹇紝鍥犳濡傛灉浣犲彲浠ュ湪鎺掑簭鍓嶅厛杩涜绛涢�夛紝閭i�氬父閮芥槸姣旇緝濂界殑鍋氭硶銆傚叾浠栦笉鏄捐�屾槗瑙佺殑闂浠嶇劧鍙互閫氳繃鎭板綋鐨勫伐鍏锋潵瀹氫綅銆傚洜姝や簡瑙h繖浜涘伐鍏锋槸鏈夊ソ澶勭殑銆�
48.濡備綍鐢≒ython鏉ヨ繘琛屾煡璇㈠拰鏇挎崲涓�涓枃鏈瓧绗︿覆锛�
鍙互浣跨敤sub()鏂规硶鏉ヨ繘琛屾煡璇㈠拰鏇挎崲锛宻ub鏂规硶鐨勬牸寮忎负锛歴ub(replacement, string[, count=0])replacement鏄鏇挎崲鎴愮殑鏂囨湰string鏄渶瑕佽鏇挎崲鐨勬枃鏈琧ount鏄竴涓彲閫夊弬鏁帮紝鎸囨渶澶ц鏇挎崲鐨勬暟閲�49.Python閲岄潰search()鍜宮atch()鐨勫尯鍒紵
match()鍑芥暟鍙娴婻E鏄笉鏄湪string鐨勫紑濮嬩綅缃尮閰嶏紝search()浼氭壂鎻忔暣涓猻tring鏌ユ壘鍖归厤, 涔熷氨鏄match()鍙湁鍦�0浣嶇疆鍖归厤鎴愬姛鐨勮瘽鎵嶆湁杩斿洖锛屽鏋滀笉鏄紑濮嬩綅缃尮閰嶆垚鍔熺殑璇濓紝match()灏辫繑鍥瀗one
50.鐢≒ython鍖归厤HTML tag鐨勬椂鍊欙紝<.*>鍜�<.*?>鏈変粈涔堝尯鍒紵
鍓嶈�呮槸璐┆鍖归厤锛屼細浠庡ご鍒板熬鍖归厤xyz锛岃�屽悗鑰呮槸闈炶椽濠尮閰嶏紝鍙尮閰嶅埌绗竴涓� >銆�
51.Python閲岄潰濡備綍鐢熸垚闅忔満鏁帮紵
import randomrandom.random()瀹冧細杩斿洖涓�涓殢鏈虹殑0鍜�1涔嬮棿鐨勬诞鐐规暟
鎿嶄綔绯荤粺
1 select,poll鍜宔poll
鍏跺疄鎵�鏈夌殑I/O閮芥槸杞鐨勬柟娉�,鍙笉杩囧疄鐜扮殑灞傞潰涓嶅悓缃簡.
杩欎釜闂鍙兘鏈夌偣娣卞叆浜�,浣嗙浉淇¤兘鍥炵瓟鍑鸿繖涓棶棰樻槸瀵笽/O澶氳矾澶嶇敤鏈夊緢濂界殑浜嗚В浜�.鍏朵腑tornado浣跨敤鐨勫氨鏄痚poll鐨�.
鍩烘湰涓妔elect鏈�3涓己鐐�:
[if !supportLists]1.聽[endif]杩炴帴鏁板彈闄�
[if !supportLists]2.聽[endif]鏌ユ壘閰嶅閫熷害鎱�
[if !supportLists]3.聽[endif]鏁版嵁鐢卞唴鏍告嫹璐濆埌鐢ㄦ埛鎬�
poll鏀瑰杽浜嗙涓�涓己鐐�
epoll鏀逛簡涓変釜缂虹偣.
2 璋冨害绠楁硶
[if !supportLists]1.聽[endif]鍏堟潵鍏堟湇鍔�(FCFS, First Come First Serve)
[if !supportLists]2.聽[endif]鐭綔涓氫紭鍏�(SJF, Shortest Job First)
[if !supportLists]3.聽[endif]鏈�楂樹紭鍏堟潈璋冨害(Priority Scheduling)
[if !supportLists]4.聽[endif]鏃堕棿鐗囪疆杞�(RR, Round Robin)
[if !supportLists]5.聽[endif]澶氱骇鍙嶉闃熷垪璋冨害(multilevel feedback queue scheduling)
瀹炴椂璋冨害绠楁硶:
[if !supportLists]1.聽[endif]鏈�鏃╂埅鑷虫椂闂翠紭鍏圗DF
[if !supportLists]2.聽[endif]鏈�浣庢澗寮涘害浼樺厛LLF
3 姝婚攣
鍘熷洜:
[if !supportLists]1.聽[endif]绔炰簤璧勬簮
[if !supportLists]2.聽[endif]绋嬪簭鎺ㄨ繘椤哄簭涓嶅綋
蹇呰鏉′欢:
[if !supportLists]1.聽[endif]浜掓枼鏉′欢
[if !supportLists]2.聽[endif]璇锋眰鍜屼繚鎸佹潯浠�
[if !supportLists]3.聽[endif]涓嶅墺澶烘潯浠�
[if !supportLists]4.聽[endif]鐜矾绛夊緟鏉′欢
澶勭悊姝婚攣鍩烘湰鏂规硶:
[if !supportLists]1.聽[endif]棰勯槻姝婚攣(鎽掑純闄�1浠ュ鐨勬潯浠�)
[if !supportLists]2.聽[endif]閬垮厤姝婚攣(閾惰瀹剁畻娉�)
[if !supportLists]3.聽[endif]妫�娴嬫閿�(璧勬簮鍒嗛厤鍥�)
[if !supportLists]4.聽[endif]瑙i櫎姝婚攣
[if !supportLists]1.聽[endif]鍓ュず璧勬簮
[if !supportLists]2.聽[endif]鎾ら攢杩涚▼
4 绋嬪簭缂栬瘧涓庨摼鎺�
Bulid杩囩▼鍙互鍒嗚В涓�4涓楠�:棰勫鐞�(Prepressing), 缂栬瘧(Compilation)銆佹眹缂�(Assembly)銆侀摼鎺�(Linking)
浠璇█涓轰緥:
1 棰勫鐞�
棰勭紪璇戣繃绋嬩富瑕佸鐞嗛偅浜涙簮鏂囦欢涓殑浠モ��#鈥濆紑濮嬬殑棰勭紪璇戞寚浠わ紝涓昏澶勭悊瑙勫垯鏈夛細
[if !supportLists]1.聽[endif]灏嗘墍鏈夌殑鈥�#define鈥濆垹闄わ紝骞跺睍寮�鎵�鐢ㄧ殑瀹忓畾涔�
[if !supportLists]2.聽[endif]澶勭悊鎵�鏈夋潯浠堕缂栬瘧鎸囦护锛屾瘮濡傗��#if鈥濄�佲��#ifdef鈥濄�� 鈥�#elif鈥濄�佲��#endif鈥�
[if !supportLists]3.聽[endif]澶勭悊鈥�#include鈥濋缂栬瘧鎸囦护锛屽皢琚寘鍚殑鏂囦欢鎻掑叆鍒拌缂栬瘧鎸囦护鐨勪綅缃紝娉細姝よ繃绋嬫槸閫掑綊杩涜鐨�
[if !supportLists]4.聽[endif]鍒犻櫎鎵�鏈夋敞閲�
[if !supportLists]5.聽[endif]娣诲姞琛屽彿鍜屾枃浠跺悕鏍囪瘑锛屼互渚夸簬缂栬瘧鏃剁紪璇戝櫒浜х敓璋冭瘯鐢ㄧ殑琛屽彿淇℃伅浠ュ強鐢ㄤ簬缂栬瘧鏃朵骇鐢熺紪璇戦敊璇垨璀﹀憡鏃跺彲鏄剧ず琛屽彿
[if !supportLists]6.聽[endif]淇濈暀鎵�鏈夌殑#pragma缂栬瘧鍣ㄦ寚浠ゃ��
2 缂栬瘧
缂栬瘧杩囩▼灏辨槸鎶婇澶勭悊瀹岀殑鏂囦欢杩涜涓�绯诲垪鐨勮瘝娉曞垎鏋愩�佽娉曞垎鏋愩�佽涔夊垎鏋愬強浼樺寲鍚庣敓鎴愮浉搴旂殑姹囩紪浠g爜鏂囦欢銆傝繖涓繃绋嬫槸鏁翠釜绋嬪簭鏋勫缓鐨勬牳蹇冮儴鍒嗐��
3 姹囩紪
姹囩紪鍣ㄦ槸灏嗘眹缂栦唬鐮佽浆鍖栨垚鏈哄櫒鍙互鎵ц鐨勬寚浠わ紝姣忎竴鏉℃眹缂栬鍙ュ嚑涔庨兘鏄竴鏉℃満鍣ㄦ寚浠ゃ�傜粡杩囩紪璇戙�侀摼鎺ャ�佹眹缂栬緭鍑虹殑鏂囦欢鎴愪负鐩爣鏂囦欢(Object File)
4 閾炬帴
閾炬帴鐨勪富瑕佸唴瀹瑰氨鏄妸鍚勪釜妯″潡涔嬮棿鐩镐簰寮曠敤鐨勯儴鍒嗗鐞嗗ソ锛屼娇鍚勪釜妯″潡鍙互姝g‘鐨勬嫾鎺ャ�傞摼鎺ョ殑涓昏杩囩▼鍖呭潡鍦板潃鍜岀┖闂寸殑鍒嗛厤锛圓ddress and Storage Allocation锛夈�佺鍙峰喅璁�(Symbol Resolution)鍜岄噸瀹氫綅(Relocation)绛夋楠ゃ��
5 闈欐�侀摼鎺ュ拰鍔ㄦ�侀摼鎺�
闈欐�侀摼鎺ユ柟娉曪細闈欐�侀摼鎺ョ殑鏃跺�欙紝杞藉叆浠g爜灏变細鎶婄▼搴忎細鐢ㄥ埌鐨勫姩鎬佷唬鐮佹垨鍔ㄦ�佷唬鐮佺殑鍦板潃纭畾涓嬫潵闈欐�佸簱鐨勯摼鎺ュ彲浠ヤ娇鐢ㄩ潤鎬侀摼鎺ワ紝鍔ㄦ�侀摼鎺ュ簱涔熷彲浠ヤ娇鐢ㄨ繖绉嶆柟娉曢摼鎺ュ鍏ュ簱
鍔ㄦ�侀摼鎺ユ柟娉曪細浣跨敤杩欑鏂瑰紡鐨勭▼搴忓苟涓嶅湪涓�寮�濮嬪氨瀹屾垚鍔ㄦ�侀摼鎺ワ紝鑰屾槸鐩村埌鐪熸璋冪敤鍔ㄦ�佸簱浠g爜鏃讹紝杞藉叆绋嬪簭鎵嶈绠�(琚皟鐢ㄧ殑閭i儴鍒�)鍔ㄦ�佷唬鐮佺殑閫昏緫鍦板潃锛岀劧鍚庣瓑鍒版煇涓椂鍊欙紝绋嬪簭鍙堥渶瑕佽皟鐢ㄥ彟澶栨煇鍧楀姩鎬佷唬鐮佹椂锛岃浇鍏ョ▼搴忓張鍘昏绠楄繖閮ㄥ垎浠g爜鐨勯�昏緫鍦板潃锛屾墍浠ワ紝杩欑鏂瑰紡浣跨▼搴忓垵濮嬪寲鏃堕棿杈冪煭锛屼絾杩愯鏈熼棿鐨勬�ц兘姣斾笉涓婇潤鎬侀摼鎺ョ殑绋嬪簭
6 铏氭嫙鍐呭瓨鎶�鏈�
铏氭嫙瀛樺偍鍣ㄦ槸鍊煎叿鏈夎姹傝皟鍏ュ姛鑳藉拰缃崲鍔熻兘,鑳戒粠閫昏緫涓婂鍐呭瓨瀹归噺鍔犱互鎵╁厖鐨勪竴绉嶅瓨鍌ㄧ郴缁�.
7 鍒嗛〉鍜屽垎娈�
鍒嗛〉: 鐢ㄦ埛绋嬪簭鐨勫湴鍧�绌洪棿琚垝鍒嗘垚鑻ュ共鍥哄畾澶у皬鐨勫尯鍩燂紝绉颁负鈥滈〉鈥濓紝鐩稿簲鍦帮紝鍐呭瓨绌洪棿鍒嗘垚鑻ュ共涓墿鐞嗗潡锛岄〉鍜屽潡鐨勫ぇ灏忕浉绛夈�傚彲灏嗙敤鎴风▼搴忕殑浠讳竴椤垫斁鍦ㄥ唴瀛樼殑浠讳竴鍧椾腑锛屽疄鐜颁簡绂绘暎鍒嗛厤銆�
鍒嗘: 灏嗙敤鎴风▼搴忓湴鍧�绌洪棿鍒嗘垚鑻ュ共涓ぇ灏忎笉绛夌殑娈碉紝姣忔鍙互瀹氫箟涓�缁勭浉瀵瑰畬鏁寸殑閫昏緫淇℃伅銆傚瓨鍌ㄥ垎閰嶆椂锛屼互娈典负鍗曚綅锛屾涓庢鍦ㄥ唴瀛樹腑鍙互涓嶇浉閭绘帴锛屼篃瀹炵幇浜嗙鏁e垎閰嶃��
鍒嗛〉涓庡垎娈电殑涓昏鍖哄埆
[if !supportLists]1.聽[endif]椤垫槸淇℃伅鐨勭墿鐞嗗崟浣�,鍒嗛〉鏄负浜嗗疄鐜伴潪杩炵画鍒嗛厤,浠ヤ究瑙e喅鍐呭瓨纰庣墖闂,鎴栬�呰鍒嗛〉鏄敱浜庣郴缁熺鐞嗙殑闇�瑕�.娈垫槸淇℃伅鐨勯�昏緫鍗曚綅,瀹冨惈鏈変竴缁勬剰涔夌浉瀵瑰畬鏁寸殑淇℃伅,鍒嗘鐨勭洰鐨勬槸涓轰簡鏇村ソ鍦板疄鐜板叡浜�,婊¤冻鐢ㄦ埛鐨勯渶瑕�.
[if !supportLists]2.聽[endif]椤电殑澶у皬鍥哄畾,鐢辩郴缁熺‘瀹�,灏嗛�昏緫鍦板潃鍒掑垎涓洪〉鍙峰拰椤靛唴鍦板潃鏄敱鏈哄櫒纭欢瀹炵幇鐨�.鑰屾鐨勯暱搴﹀嵈涓嶅浐瀹�,鍐冲畾浜庣敤鎴锋墍缂栧啓鐨勭▼搴�,閫氬父鐢辩紪璇戠▼搴忓湪瀵规簮绋嬪簭杩涜缂栬瘧鏃舵牴鎹俊鎭殑鎬ц川鏉ュ垝鍒�.
[if !supportLists]3.聽[endif]鍒嗛〉鐨勪綔涓氬湴鍧�绌洪棿鏄竴缁寸殑.鍒嗘鐨勫湴鍧�绌洪棿鏄簩缁寸殑.
8 椤甸潰缃崲绠楁硶
[if !supportLists]1.聽[endif]鏈�浣崇疆鎹㈢畻娉昈PT:涓嶅彲鑳藉疄鐜�
[if !supportLists]2.聽[endif]鍏堣繘鍏堝嚭FIFO
[if !supportLists]3.聽[endif]鏈�杩戞渶涔呮湭浣跨敤绠楁硶LRU:鏈�杩戜竴娈垫椂闂撮噷鏈�涔呮病鏈変娇鐢ㄨ繃鐨勯〉闈簣浠ョ疆鎹�.
[if !supportLists]4.聽[endif]clock绠楁硶
9 杈规部瑙﹀彂鍜屾按骞宠Е鍙�
杈圭紭瑙﹀彂鏄寚姣忓綋鐘舵�佸彉鍖栨椂鍙戠敓涓�涓猧o 浜嬩欢锛屾潯浠惰Е鍙戞槸鍙婊¤冻鏉′欢灏卞彂鐢熶竴涓� io 浜嬩欢
鏁版嵁搴�
1 浜嬪姟
鏁版嵁搴撲簨鍔�(Database Transaction) 锛屾槸鎸囦綔涓哄崟涓�昏緫宸ヤ綔鍗曞厓鎵ц鐨勪竴绯诲垪鎿嶄綔锛岃涔堝畬鍏ㄥ湴鎵ц锛岃涔堝畬鍏ㄥ湴涓嶆墽琛屻��
2 鏁版嵁搴撶储寮�
绱㈠紩鏄鏁版嵁搴撹〃涓竴鍒楁垨澶氬垪鐨勫�艰繘琛屾帓搴忕殑涓�绉嶇粨鏋勶紝浣跨敤绱㈠紩鍙揩閫熻闂暟鎹簱琛ㄤ腑鐨勭壒瀹氫俊鎭��
绱㈠紩鍒嗕负鑱氱皣绱㈠紩鍜岄潪鑱氱皣绱㈠紩涓ょ锛岃仛绨囩储寮曟槸鎸夌収鏁版嵁瀛樻斁鐨勭墿鐞嗕綅缃负椤哄簭鐨勶紝鑰岄潪鑱氱皣绱㈠紩灏变笉涓�鏍蜂簡锛涜仛绨囩储寮曡兘鎻愰珮澶氳妫�绱㈢殑閫熷害锛岃�岄潪鑱氱皣绱㈠紩瀵逛簬鍗曡鐨勬绱㈠緢蹇��
鎺ㄨ崘: http://tech.meituan.com/mysql-index.html
3 Redis鍘熺悊
4 涔愯閿佸拰鎮茶閿�
鎮茶閿侊細鍋囧畾浼氬彂鐢熷苟鍙戝啿绐侊紝灞忚斀涓�鍒囧彲鑳借繚鍙嶆暟鎹畬鏁存�х殑鎿嶄綔
涔愯閿侊細鍋囪涓嶄細鍙戠敓骞跺彂鍐茬獊锛屽彧鍦ㄦ彁浜ゆ搷浣滄椂妫�鏌ユ槸鍚﹁繚鍙嶆暟鎹畬鏁存�с��
5 MVCC
澶у鏁扮殑MySQL浜嬪姟鍨嬪瓨鍌ㄥ紩鎿庯紝濡侷nnoDB锛孎alcon浠ュ強PBXT閮戒笉浣跨敤涓�绉嶇畝鍗曠殑琛岄攣鏈哄埗銆備簨瀹炰笂锛屼粬浠兘鍜屽彟澶栦竴绉嶇敤鏉ュ鍔犲苟鍙戞�х殑琚О涓衡�滃鐗堟湰骞跺彂鎺у埗锛圡VCC锛夆�濈殑鏈哄埗鏉ヤ竴璧蜂娇鐢ㄣ�侻VCC涓嶅彧浣跨敤鍦∕ySQL涓紝Oracle銆丳ostgreSQL锛屼互鍙婂叾浠栦竴浜涙暟鎹簱绯荤粺涔熷悓鏍蜂娇鐢ㄥ畠銆�
6 MyISAM鍜孖nnoDB
MyISAM 閫傚悎浜庝竴浜涢渶瑕佸ぇ閲忔煡璇㈢殑搴旂敤锛屼絾鍏跺浜庢湁澶ч噺鍐欐搷浣滃苟涓嶆槸寰堝ソ銆傜敋鑷充綘鍙槸闇�瑕乽pdate涓�涓瓧娈碉紝鏁翠釜琛ㄩ兘浼氳閿佽捣鏉ワ紝鑰屽埆鐨勮繘绋嬶紝灏辩畻鏄杩涚▼閮芥棤娉曟搷浣滅洿鍒拌鎿嶄綔瀹屾垚銆傚彟澶栵紝MyISAM 瀵逛簬 SELECT COUNT(*) 杩欑被鐨勮绠楁槸瓒呭揩鏃犳瘮鐨勩��
InnoDB 鐨勮秼鍔夸細鏄竴涓潪甯稿鏉傜殑瀛樺偍寮曟搸锛屽浜庝竴浜涘皬鐨勫簲鐢紝瀹冧細姣� MyISAM 杩樻參銆備粬鏄畠鏀寔鈥滆閿佲�� 锛屼簬鏄湪鍐欐搷浣滄瘮杈冨鐨勬椂鍊欙紝浼氭洿浼樼銆傚苟涓旓紝浠栬繕鏀寔鏇村鐨勯珮绾у簲鐢紝姣斿锛氫簨鍔°��
缃戠粶
1 涓夋鎻℃墜
[if !supportLists]1.聽[endif]瀹㈡埛绔�氳繃鍚戞湇鍔″櫒绔彂閫佷竴涓猄YN鏉ュ垱寤轰竴涓富鍔ㄦ墦寮�锛屼綔涓轰笁璺彙鎵嬬殑涓�閮ㄥ垎銆傚鎴风鎶婅繖娈佃繛鎺ョ殑搴忓彿璁惧畾涓洪殢鏈烘暟 A銆�
[if !supportLists]2.聽[endif]鏈嶅姟鍣ㄧ搴斿綋涓轰竴涓悎娉曠殑SYN鍥為�佷竴涓猄YN/ACK銆侫CK 鐨勭‘璁ょ爜搴斾负 A+1锛孲YN/ACK 鍖呮湰韬張鏈変竴涓殢鏈哄簭鍙� B銆�
[if !supportLists]3.聽[endif]鏈�鍚庯紝瀹㈡埛绔啀鍙戦�佷竴涓狝CK銆傚綋鏈嶅姟绔彈鍒拌繖涓狝CK鐨勬椂鍊欙紝灏卞畬鎴愪簡涓夎矾鎻℃墜锛屽苟杩涘叆浜嗚繛鎺ュ垱寤虹姸鎬併�傛鏃跺寘搴忓彿琚瀹氫负鏀跺埌鐨勭‘璁ゅ彿 A+1锛岃�屽搷搴斿垯涓� B+1銆�
2 鍥涙鎸ユ墜
CP鐨勮繛鎺ョ殑鎷嗛櫎闇�瑕佸彂閫佸洓涓寘锛屽洜姝ょО涓哄洓娆℃尌鎵�(four-way handshake)銆傚鎴风鎴栨湇鍔″櫒鍧囧彲涓诲姩鍙戣捣鎸ユ墜鍔ㄤ綔锛屽湪socket缂栫▼涓紝浠讳綍涓�鏂规墽琛宑lose()鎿嶄綔鍗冲彲浜х敓鎸ユ墜鎿嶄綔銆�
锛�1锛夊鎴风A鍙戦�佷竴涓狥IN锛岀敤鏉ュ叧闂鎴稟鍒版湇鍔″櫒B鐨勬暟鎹紶閫併��
锛�2锛夋湇鍔″櫒B鏀跺埌杩欎釜FIN锛屽畠鍙戝洖涓�涓狝CK锛岀‘璁ゅ簭鍙蜂负鏀跺埌鐨勫簭鍙峰姞1銆傚拰SYN涓�鏍凤紝涓�涓狥IN灏嗗崰鐢ㄤ竴涓簭鍙枫��
锛�3锛夋湇鍔″櫒B鍏抽棴涓庡鎴风A鐨勮繛鎺ワ紝鍙戦�佷竴涓狥IN缁欏鎴风A銆�
锛�4锛夊鎴风A鍙戝洖ACK鎶ユ枃纭锛屽苟灏嗙‘璁ゅ簭鍙疯缃负鏀跺埌搴忓彿鍔�1銆�
3 ARP鍗忚
鍦板潃瑙f瀽鍗忚(Address Resolution Protocol): 鏍规嵁IP鍦板潃鑾峰彇鐗╃悊鍦板潃鐨勪竴涓猅CP/IP鍗忚
4 urllib鍜寀rllib2鐨勫尯鍒�
杩欎釜闈㈣瘯瀹樼‘瀹為棶杩�,褰撴椂绛旂殑urllib2鍙互Post鑰寀rllib涓嶅彲浠�.
[if !supportLists]1.聽[endif]urllib鎻愪緵urlencode鏂规硶鐢ㄦ潵GET鏌ヨ瀛楃涓茬殑浜х敓锛岃�寀rllib2娌℃湁銆傝繖鏄负浣晆rllib甯稿拰urllib2涓�璧蜂娇鐢ㄧ殑鍘熷洜銆�
[if !supportLists]2.聽[endif]urllib2鍙互鎺ュ彈涓�涓猂equest绫荤殑瀹炰緥鏉ヨ缃甎RL璇锋眰鐨刪eaders锛寀rllib浠呭彲浠ユ帴鍙桿RL銆傝繖鎰忓懗鐫�锛屼綘涓嶅彲浠ヤ吉瑁呬綘鐨刄ser Agent瀛楃涓茬瓑銆�
5 Post鍜孏et鍖哄埆
GET鍚庨��鎸夐挳/鍒锋柊鏃犲锛孭OST鏁版嵁浼氳閲嶆柊鎻愪氦锛堟祻瑙堝櫒搴旇鍛婄煡鐢ㄦ埛鏁版嵁浼氳閲嶆柊鎻愪氦锛夈��
GET涔︾鍙敹钘忥紝POST涓轰功绛句笉鍙敹钘忋��
GET鑳借缂撳瓨锛孭OST涓嶈兘缂撳瓨 銆�
GET缂栫爜绫诲瀷application/x-www-form-url锛孭OST缂栫爜绫诲瀷encodedapplication/x-www-form-urlencoded 鎴� multipart/form-data銆備负浜岃繘鍒舵暟鎹娇鐢ㄥ閲嶇紪鐮併��
GET鍘嗗彶鍙傛暟淇濈暀鍦ㄦ祻瑙堝櫒鍘嗗彶涓�侾OST鍙傛暟涓嶄細淇濆瓨鍦ㄦ祻瑙堝櫒鍘嗗彶涓��
GET瀵规暟鎹暱搴︽湁闄愬埗锛屽綋鍙戦�佹暟鎹椂锛孏ET 鏂规硶鍚� URL 娣诲姞鏁版嵁锛沀RL 鐨勯暱搴︽槸鍙楅檺鍒剁殑锛圲RL 鐨勬渶澶ч暱搴︽槸 2048 涓瓧绗︼級銆侾OST鏃犻檺鍒躲��
GET鍙厑璁� ASCII 瀛楃銆侾OST娌℃湁闄愬埗銆備篃鍏佽浜岃繘鍒舵暟鎹��
涓嶱OST 鐩告瘮锛孏ET 鐨勫畨鍏ㄦ�ц緝宸紝鍥犱负鎵�鍙戦�佺殑鏁版嵁鏄� URL 鐨勪竴閮ㄥ垎銆傚湪鍙戦�佸瘑鐮佹垨鍏朵粬鏁忔劅淇℃伅鏃剁粷涓嶈浣跨敤 GET 锛丳OST 姣� GET 鏇村畨鍏紝鍥犱负鍙傛暟涓嶄細琚繚瀛樺湪娴忚鍣ㄥ巻鍙叉垨 web 鏈嶅姟鍣ㄦ棩蹇椾腑銆�
GET鐨勬暟鎹湪 URL 涓鎵�鏈変汉閮芥槸鍙鐨勩�侾OST鐨勬暟鎹笉浼氭樉绀哄湪 URL 涓��
6 Cookie鍜孲ession
聽CookieSession
鍌ㄥ瓨浣嶇疆瀹㈡埛绔湇鍔″櫒绔�
鐩殑璺熻釜浼氳瘽锛屼篃鍙互淇濆瓨鐢ㄦ埛鍋忓ソ璁剧疆鎴栬�呬繚瀛樼敤鎴峰悕瀵嗙爜绛夎窡韪細璇�
瀹夊叏鎬т笉瀹夊叏瀹夊叏
session鎶�鏈槸瑕佷娇鐢ㄥ埌cookie鐨勶紝涔嬫墍浠ュ嚭鐜皊ession鎶�鏈紝涓昏鏄负浜嗗畨鍏ㄣ��
7 apache鍜宯ginx鐨勫尯鍒�
nginx 鐩稿 apache 鐨勪紭鐐癸細
[if !supportLists]路聽[endif]杞婚噺绾э紝鍚屾牱璧穡eb 鏈嶅姟锛屾瘮apache 鍗犵敤鏇村皯鐨勫唴瀛樺強璧勬簮
[if !supportLists]路聽[endif]鎶楀苟鍙戯紝nginx 澶勭悊璇锋眰鏄紓姝ラ潪闃诲鐨勶紝鏀寔鏇村鐨勫苟鍙戣繛鎺ワ紝鑰宎pache 鍒欐槸闃诲鍨嬬殑锛屽湪楂樺苟鍙戜笅nginx 鑳戒繚鎸佷綆璧勬簮浣庢秷鑰楅珮鎬ц兘
[if !supportLists]路聽[endif]閰嶇疆绠�娲�
[if !supportLists]路聽[endif]楂樺害妯″潡鍖栫殑璁捐锛岀紪鍐欐ā鍧楃浉瀵圭畝鍗�
[if !supportLists]路聽[endif]绀惧尯娲昏穬
apache 鐩稿nginx 鐨勪紭鐐癸細
[if !supportLists]路聽[endif]rewrite 锛屾瘮nginx 鐨剅ewrite 寮哄ぇ
[if !supportLists]路聽[endif]妯″潡瓒呭锛屽熀鏈兂鍒扮殑閮藉彲浠ユ壘鍒�
[if !supportLists]路聽[endif]灏慴ug 锛宯ginx 鐨刡ug 鐩稿杈冨
[if !supportLists]路聽[endif]瓒呯ǔ瀹�
8 缃戠珯鐢ㄦ埛瀵嗙爜淇濆瓨
[if !supportLists]1.聽[endif]鏄庢枃淇濆瓨
[if !supportLists]2.聽[endif]鏄庢枃hash鍚庝繚瀛�,濡俶d5
[if !supportLists]3.聽[endif]MD5+Salt鏂瑰紡,杩欎釜salt鍙互闅忔満
[if !supportLists]4.聽[endif]鐭ヤ箮浣跨敤浜咮crypy(濂藉儚)鍔犲瘑
9 HTTP鍜孒TTPS
HTTPS锛堝叏绉帮細Hypertext Transfer Protocol over Secure Socket Layer锛夛紝鏄互瀹夊叏涓虹洰鏍囩殑HTTP閫氶亾锛岀畝鍗曡鏄疕TTP鐨勫畨鍏ㄧ増銆傚嵆HTTP涓嬪姞鍏SL灞傦紝HTTPS鐨勫畨鍏ㄥ熀纭�鏄疭SL锛屽洜姝ゅ姞瀵嗙殑璇︾粏鍐呭灏遍渶瑕丼SL銆� 瀹冩槸涓�涓猆RI scheme锛堟娊璞℃爣璇嗙浣撶郴锛夛紝鍙ユ硶绫诲悓http:浣撶郴銆傜敤浜庡畨鍏ㄧ殑HTTP鏁版嵁浼犺緭銆俬ttps:URL琛ㄦ槑瀹冧娇鐢ㄤ簡HTTP锛屼絾HTTPS瀛樺湪涓嶅悓浜嶩TTP鐨勯粯璁ょ鍙e強涓�涓姞瀵�/韬唤楠岃瘉灞傦紙鍦℉TTP涓嶵CP涔嬮棿锛夈�傝繖涓郴缁熺殑鏈�鍒濈爺鍙戠敱缃戞櫙鍏徃杩涜锛屾彁渚涗簡韬唤楠岃瘉涓庡姞瀵嗛�氳鏂规硶锛岀幇鍦ㄥ畠琚箍娉涚敤浜庝竾缁寸綉涓婂畨鍏ㄦ晱鎰熺殑閫氳锛屼緥濡備氦鏄撴敮浠樻柟闈€��
瓒呮枃鏈紶杈撳崗璁�(HTTP-Hypertext transfer protocol) 鏄竴绉嶈缁嗚瀹氫簡娴忚鍣ㄥ拰涓囩淮缃戞湇鍔″櫒涔嬮棿浜掔浉閫氫俊鐨勮鍒欙紝閫氳繃鍥犵壒缃戜紶閫佷竾缁寸綉鏂囨。鐨勬暟鎹紶閫佸崗璁��
10 XSRF鍜孹SS
[if !supportLists]路聽[endif]CSRF(Cross-site request forgery)璺ㄧ珯璇锋眰浼��
[if !supportLists]路聽[endif]XSS(Cross Site Scripting)璺ㄧ珯鑴氭湰鏀诲嚮
CSRF閲嶇偣鍦ㄨ姹�,XSS閲嶇偣鍦ㄨ剼鏈�
11 RESTful鏋舵瀯(SOAP,RPC)
鎺ㄨ崘: http://www.ruanyifeng.com/blog/2011/09/restful.html
12 SOAP
SOAP锛堝師涓篠imple Object Access Protocol鐨勯瀛楁瘝缂╁啓锛屽嵆绠�鍗曞璞¤闂崗璁級鏄氦鎹㈡暟鎹殑涓�绉嶅崗璁鑼冿紝浣跨敤鍦ㄨ绠楁満缃戠粶Web鏈嶅姟锛坵eb service锛変腑锛屼氦鎹㈠甫缁撴瀯淇℃伅銆係OAP涓轰簡绠�鍖栫綉椤垫湇鍔″櫒锛圵eb Server锛変粠XML鏁版嵁搴撲腑鎻愬彇鏁版嵁鏃讹紝鑺傜渷鍘绘牸寮忓寲椤甸潰鏃堕棿锛屼互鍙婁笉鍚屽簲鐢ㄧ▼搴忎箣闂存寜鐓TTP閫氫俊鍗忚锛岄伒浠嶺ML鏍煎紡鎵ц璧勬枡浜掓崲锛屼娇鍏舵娊璞′簬璇█瀹炵幇銆佸钩鍙板拰纭欢銆�
13 RPC
RPC锛圧emote Procedure Call Protocol锛夆�斺�旇繙绋嬭繃绋嬭皟鐢ㄥ崗璁紝瀹冩槸涓�绉嶉�氳繃缃戠粶浠庤繙绋嬭绠楁満绋嬪簭涓婅姹傛湇鍔★紝鑰屼笉闇�瑕佷簡瑙e簳灞傜綉缁滄妧鏈殑鍗忚銆俁PC鍗忚鍋囧畾鏌愪簺浼犺緭鍗忚鐨勫瓨鍦紝濡俆CP鎴朥DP锛屼负閫氫俊绋嬪簭涔嬮棿鎼哄甫淇℃伅鏁版嵁銆傚湪OSI缃戠粶閫氫俊妯″瀷涓紝RPC璺ㄨ秺浜嗕紶杈撳眰鍜屽簲鐢ㄥ眰銆俁PC浣垮緱寮�鍙戝寘鎷綉缁滃垎甯冨紡澶氱▼搴忓湪鍐呯殑搴旂敤绋嬪簭鏇村姞瀹规槗銆�
鎬荤粨:鏈嶅姟鎻愪緵鐨勪袱澶ф祦娲�.浼犵粺鎰忎箟浠ユ柟娉曡皟鐢ㄤ负瀵煎悜閫氱ОRPC銆備负浜嗕紒涓歋OA,鑻ュ共鍘傚晢鑱斿悎鎺ㄥ嚭webservice,鍒跺畾浜唚sdl鎺ュ彛瀹氫箟,浼犺緭soap.褰撲簰鑱旂綉鏃朵唬,鑷冭偪SOA琚畝鍖栦负http+xml/json.浣嗘槸绠�鍖栧嚭鐜板悇绉嶆贩涔便�備互璧勬簮涓哄鍚�,浠讳綍鎿嶄綔鏃犻潪鏄璧勬簮鐨勫鍒犳敼鏌ワ紝浜庢槸缁熶竴鐨凴EST鍑虹幇浜�.
杩涘寲鐨勯『搴�: RPC -> SOAP -> RESTful
14 CGI鍜學SGI
CGI鏄�氱敤缃戝叧鎺ュ彛锛屾槸杩炴帴web鏈嶅姟鍣ㄥ拰搴旂敤绋嬪簭鐨勬帴鍙o紝鐢ㄦ埛閫氳繃CGI鏉ヨ幏鍙栧姩鎬佹暟鎹垨鏂囦欢绛夈�侰GI绋嬪簭鏄竴涓嫭绔嬬殑绋嬪簭锛屽畠鍙互鐢ㄥ嚑涔庢墍鏈夎瑷�鏉ュ啓锛屽寘鎷琾erl锛宑锛宭ua锛宲ython绛夌瓑銆�
WSGI, Web Server Gateway Interface锛屾槸Python搴旂敤绋嬪簭鎴栨鏋跺拰Web鏈嶅姟鍣ㄤ箣闂寸殑涓�绉嶆帴鍙o紝WSGI鐨勫叾涓竴涓洰鐨勫氨鏄鐢ㄦ埛鍙互鐢ㄧ粺涓�鐨勮瑷�(Python)缂栧啓鍓嶅悗绔��
16 涓棿浜烘敾鍑�
鍦℅FW閲屽薄瑙佷笉椴滅殑,鍛靛懙.
涓棿浜烘敾鍑伙紙Man-in-the-middle attack锛岄�氬父缂╁啓涓篗ITM锛夋槸鎸囨敾鍑昏�呬笌閫氳鐨勪袱绔垎鍒垱寤虹嫭绔嬬殑鑱旂郴锛屽苟浜ゆ崲鍏舵墍鏀跺埌鐨勬暟鎹紝浣块�氳鐨勪袱绔涓轰粬浠鍦ㄩ�氳繃涓�涓瀵嗙殑杩炴帴涓庡鏂圭洿鎺ュ璇濓紝浣嗕簨瀹炰笂鏁翠釜浼氳瘽閮借鏀诲嚮鑰呭畬鍏ㄦ帶鍒躲��
17 c10k闂
鎵�璋揷10k闂锛屾寚鐨勬槸鏈嶅姟鍣ㄥ悓鏃舵敮鎸佹垚鍗冧笂涓囦釜瀹㈡埛绔殑闂锛屼篃灏辨槸concurrent 10 000 connection锛堣繖涔熸槸c10k杩欎釜鍚嶅瓧鐨勭敱鏉ワ級銆�
18 socket
Socket=Ip address+ TCP/UDP + port
19 娴忚鍣ㄧ紦瀛�
鎺ㄨ崘:http://web.jobbole.com/84367/
娴忚鍣ㄧ紦瀛樻満鍒讹紝鍏跺疄涓昏灏辨槸HTTP鍗忚瀹氫箟鐨勭紦瀛樻満鍒讹紙濡傦細 Expires锛� Cache-control绛夛級
Expires绛栫暐
Expires鏄疻eb鏈嶅姟鍣ㄥ搷搴旀秷鎭ご瀛楁锛屽湪鍝嶅簲http璇锋眰鏃跺憡璇夋祻瑙堝櫒鍦ㄨ繃鏈熸椂闂村墠娴忚鍣ㄥ彲浠ョ洿鎺ヤ粠娴忚鍣ㄧ紦瀛樺彇鏁版嵁锛岃�屾棤闇�鍐嶆璇锋眰銆�
Cache-control绛栫暐锛堥噸鐐瑰叧娉級
Cache-Control涓嶦xpires鐨勪綔鐢ㄤ竴鑷达紝閮芥槸鎸囨槑褰撳墠璧勬簮鐨勬湁鏁堟湡锛屾帶鍒舵祻瑙堝櫒鏄惁鐩存帴浠庢祻瑙堝櫒缂撳瓨鍙栨暟鎹繕鏄噸鏂板彂璇锋眰鍒版湇鍔″櫒鍙栨暟鎹�傚彧涓嶈繃Cache-Control鐨勯�夋嫨鏇村锛岃缃洿缁嗚嚧锛屽鏋滃悓鏃惰缃殑璇濓紝鍏朵紭鍏堢骇楂樹簬Expires聽
20 HTTP1.0鍜孒TTP1.1
鎺ㄨ崘: http://blog.csdn.net/elifefly/article/details/3964766
[if !supportLists]1.聽[endif]璇锋眰澶碒ost瀛楁,涓�涓湇鍔″櫒澶氫釜缃戠珯
[if !supportLists]2.聽[endif]闀块摼鎺�
[if !supportLists]3.聽[endif]鏂囦欢鏂偣缁紶
[if !supportLists]4.聽[endif]韬唤璁よ瘉,鐘舵�佺鐞�,Cache缂撳瓨
21 Ajax
AJAX,Asynchronous JavaScript and XML锛堝紓姝ョ殑 JavaScript 鍜� XML锛�, 鏄笌鍦ㄤ笉閲嶆柊鍔犺浇鏁翠釜椤甸潰鐨勬儏鍐典笅锛屼笌鏈嶅姟鍣ㄤ氦鎹㈡暟鎹苟鏇存柊閮ㄥ垎缃戦〉鐨勬妧鏈��
鏁版嵁缁撴瀯
1 绾㈤粦鏍�
绾㈤粦鏍戜笌AVL鐨勬瘮杈冿細
AVL鏄弗鏍煎钩琛℃爲锛屽洜姝ゅ湪澧炲姞鎴栬�呭垹闄よ妭鐐圭殑鏃跺�欙紝鏍规嵁涓嶅悓鎯呭喌锛屾棆杞殑娆℃暟姣旂孩榛戞爲瑕佸锛�
绾㈤粦鏄敤闈炰弗鏍肩殑骞宠 鏉ユ崲鍙栧鍒犺妭鐐规椂鍊欐棆杞鏁扮殑闄嶄綆锛�
鎵�浠ョ畝鍗曡锛屽鏋滀綘鐨勫簲鐢ㄤ腑锛屾悳绱㈢殑娆℃暟杩滆繙澶т簬鎻掑叆鍜屽垹闄わ紝閭d箞閫夋嫨AVL锛屽鏋滄悳绱紝鎻掑叆鍒犻櫎娆℃暟鍑犱箮宸笉澶氾紝搴旇閫夋嫨RB銆�
1 鍙伴樁闂/鏂愭尝绾虫寛
涓�鍙潚铔欎竴娆″彲浠ヨ烦涓�1绾у彴闃讹紝涔熷彲浠ヨ烦涓�2绾с�傛眰璇ラ潚铔欒烦涓婁竴涓猲绾х殑鍙伴樁鎬诲叡鏈夊灏戠璺虫硶銆�
1fib聽=聽lambda聽n:聽n聽if聽n聽<=聽2聽else聽fib(n聽-聽1)聽+聽fib(n聽-聽2)
绗簩绉嶈蹇嗘柟娉�
聽def聽memo(func):
聽聽聽聽cache聽=聽{}
聽聽聽聽def聽wrap(*args):
聽聽聽聽聽聽聽聽if聽args not聽in聽cache:
聽聽聽聽聽聽聽聽聽聽聽聽cache[args]聽=聽func(*args)
聽聽聽聽聽聽聽聽return聽cache[args]
聽聽聽聽return聽wrap
@聽memo
def聽fib(i):
聽聽聽聽if聽i聽<聽2:
聽聽聽聽聽聽聽聽return聽1
聽聽聽聽return聽fib(i-1)聽+聽fib(i-2)
绗笁绉嶆柟娉�
def聽fib(n):
聽聽聽聽a,聽b聽=聽0,聽1
聽聽聽聽for聽_聽in聽xrange(n):
聽聽聽聽聽聽聽聽a,聽b聽=聽b,聽a聽+聽b
聽聽聽聽return聽b
2 鍙樻�佸彴闃堕棶棰�
涓�鍙潚铔欎竴娆″彲浠ヨ烦涓�1绾у彴闃讹紝涔熷彲浠ヨ烦涓�2绾р�︹�﹀畠涔熷彲浠ヨ烦涓妌绾с�傛眰璇ラ潚铔欒烦涓婁竴涓猲绾х殑鍙伴樁鎬诲叡鏈夊灏戠璺虫硶銆�
1fib聽=聽lambda聽n:聽n聽if聽n聽<聽2聽else聽2聽*聽fib(n聽-聽1)
3 鐭╁舰瑕嗙洊
鎴戜滑鍙互鐢�2*1鐨勫皬鐭╁舰妯潃鎴栬�呯珫鐫�鍘昏鐩栨洿澶х殑鐭╁舰銆傝闂敤n涓�2*1鐨勫皬鐭╁舰鏃犻噸鍙犲湴瑕嗙洊涓�涓�2*n鐨勫ぇ鐭╁舰锛屾�诲叡鏈夊灏戠鏂规硶锛�
绗�2*n涓煩褰㈢殑瑕嗙洊鏂规硶绛変簬绗�2*(n-1)鍔犱笂绗�2*(n-2)鐨勬柟娉曘��
1f聽=聽lambda聽n:聽1聽if聽n聽<聽2聽else聽f(n聽-聽1)聽+聽f(n聽-聽2)
4 鏉ㄦ皬鐭╅樀鏌ユ壘
鍦ㄤ竴涓猰琛宯鍒椾簩缁存暟缁勪腑锛屾瘡涓�琛岄兘鎸夌収浠庡乏鍒板彸閫掑鐨勯『搴忔帓搴忥紝姣忎竴鍒楅兘鎸夌収浠庝笂鍒颁笅閫掑鐨勯『搴忔帓搴忋�傝瀹屾垚涓�涓嚱鏁帮紝杈撳叆杩欐牱鐨勪竴涓簩缁存暟缁勫拰涓�涓暣鏁帮紝鍒ゆ柇鏁扮粍涓槸鍚﹀惈鏈夎鏁存暟銆�
5 鍘婚櫎鍒楄〃涓殑閲嶅鍏冪礌
鐢ㄩ泦鍚�
1list(set(l))
鐢ㄥ瓧鍏�
l1聽=聽['b','c','d','b','c','a','a']
l2聽=聽{}.fromkeys(l1).keys()
print聽l2
鐢ㄥ瓧鍏稿苟淇濇寔椤哄簭
l1聽=聽['b','c','d','b','c','a','a']
l2聽=聽list(set(l1))
l2.sort(key=l1.index)
print聽l2
l1聽=聽['b','c','d','b','c','a','a']
l2聽=聽[]
[l2.append(i)聽for聽i聽in聽l1 if聽not聽i聽in聽l2]
闈㈣瘯瀹樻彁鍒扮殑,鍏堟帓搴忕劧鍚庡垹闄�.
6 閾捐〃鎴愬璋冩崲
1->2->3->4杞崲鎴�2->1->4->3.
聽class聽ListNode:
聽聽聽聽def聽__init__(self,聽x):
聽聽聽聽聽聽聽聽self.val聽=聽x
聽聽聽聽聽聽聽聽self.next聽=聽None
class聽Solution:
聽聽聽聽# @param a ListNode
聽聽聽聽# @return a ListNode
聽聽聽聽def聽swapPairs(self,聽head):
聽聽聽聽聽聽聽聽if聽head聽!=聽None聽and聽head.next聽!=聽None:
聽聽聽聽聽聽聽聽聽聽聽聽next聽=聽head.next
聽聽聽聽聽聽聽聽聽聽聽聽head.next聽=聽self.swapPairs(next.next)
聽聽聽聽聽聽聽聽聽聽聽聽next.next聽=聽head
聽聽聽聽聽聽聽聽聽聽聽聽return聽next
聽聽聽聽聽聽聽聽return聽head
7 鍒涘缓瀛楀吀鐨勬柟娉�
1 鐩存帴鍒涘缓
1dict聽=聽{'name':'earth',聽'port':'80'}
2 宸ュ巶鏂规硶
items=[('name','earth'),('port','80')]
dict2=dict(items)
dict1=dict((['name','earth'],['port','80']))
3 fromkeys()鏂规硶
dict1={}.fromkeys(('x','y'),-1)
dict={'x':-1,'y':-1}
dict2={}.fromkeys(('x','y'))
dict2={'x':None,聽'y':None}
8 鍚堝苟涓や釜鏈夊簭鍒楄〃
鐭ヤ箮杩滅▼闈㈣瘯瑕佹眰缂栫▼
灏鹃�掑綊
聽def聽_recursion_merge_sort2(l1,聽l2,聽tmp):
聽聽聽聽if聽len(l1)聽==聽0聽or聽len(l2)聽==聽0:
聽聽聽聽聽聽聽聽tmp.extend(l1)
聽聽聽聽聽聽聽聽tmp.extend(l2)
聽聽聽聽聽聽聽聽return聽tmp
聽聽聽聽else:
聽聽聽聽聽聽聽聽if聽l1[0]聽<聽l2[0]:
聽聽聽聽聽聽聽聽聽聽聽聽tmp.append(l1[0])
聽聽聽聽聽聽聽聽聽聽聽聽del聽l1[0]
聽聽聽聽聽聽聽聽else:
聽聽聽聽聽聽聽聽聽聽聽聽tmp.append(l2[0])
聽聽聽聽聽聽聽聽聽聽聽聽del聽l2[0]
聽聽聽聽聽聽聽聽return聽_recursion_merge_sort2(l1,聽l2,聽tmp)
def聽recursion_merge_sort2(l1,聽l2):
聽聽聽聽return聽_recursion_merge_sort2(l1,聽l2,聽[])
寰幆绠楁硶
聽def聽loop_merge_sort(l1,聽l2):
聽聽聽聽tmp聽=聽[]
聽聽聽聽while聽len(l1)聽>聽0聽and聽len(l2)聽>聽0:
聽聽聽聽聽聽聽聽if聽l1[0]聽<聽l2[0]:
聽聽聽聽聽聽聽聽聽聽聽聽tmp.append(l1[0])
聽聽聽聽聽聽聽聽聽聽聽聽del聽l1[0]
聽聽聽聽聽聽聽聽else:
聽聽聽聽聽聽聽聽聽聽聽聽tmp.append(l2[0])
聽聽聽聽聽聽聽聽聽聽聽聽del聽l2[0]
聽聽聽聽tmp.extend(l1)
聽聽聽聽tmp.extend(l2)
聽聽聽聽return聽tmp
9 浜ゅ弶閾捐〃姹備氦鐐�
鍘诲摢鍎跨殑闈㈣瘯,娌″仛鍑烘潵.
聽class聽ListNode:
聽聽聽聽def聽__init__(self,聽x):
聽聽聽聽聽聽聽聽self.val聽=聽x
聽聽聽聽聽聽聽聽self.next聽=聽None
def聽node(l1,聽l2):
聽聽聽聽length1,聽lenth2聽=聽0,聽0
聽聽聽聽#姹備袱涓摼琛ㄩ暱搴�
聽聽聽聽while聽l1.next:
聽聽聽聽聽聽聽聽l1聽=聽l1.next
聽聽聽聽聽聽聽聽length1聽+=聽1
聽聽聽聽while聽l2.next:
聽聽聽聽聽聽聽聽l2聽=聽l2.next
聽聽聽聽聽聽聽聽length2聽+=聽1
聽聽聽聽#闀跨殑閾捐〃鍏堣蛋
聽聽聽聽if聽length1聽>聽lenth2:
聽聽聽聽聽聽聽聽for聽_聽in聽range(length1聽-聽length2):
聽聽聽聽聽聽聽聽聽聽聽聽l1聽=聽l1.next
聽聽聽聽else:
聽聽聽聽聽聽聽聽for聽_聽in聽range(length2聽-聽length1):
聽聽聽聽聽聽聽聽聽聽聽聽l2聽=聽l2.next
聽聽聽聽while聽l1 and聽l2:
聽聽聽聽聽聽聽聽if聽l1.next聽==聽l2.next:
聽聽聽聽聽聽聽聽聽聽聽聽return聽l1.next
聽聽聽聽聽聽聽聽else:
聽聽聽聽聽聽聽聽聽聽聽聽l1聽=聽l1.next
聽聽聽聽聽聽聽聽聽聽聽聽l2聽=聽l2.next
10 浜屽垎鏌ユ壘
聽def聽binarySearch(l,聽t):
聽聽聽聽low,聽high聽=聽0,聽len(l)聽-聽1
聽聽聽聽while聽low聽<聽high:
聽聽聽聽聽聽聽聽print聽low,聽high
聽聽聽聽聽聽聽聽mid聽=聽(low聽+聽high)聽/聽2
聽聽聽聽聽聽聽聽if聽l[mid]聽>聽t:
聽聽聽聽聽聽聽聽聽聽聽聽high聽=聽mid
聽聽聽聽聽聽聽聽elif聽l[mid]聽<聽t:
聽聽聽聽聽聽聽聽聽聽聽聽low聽=聽mid聽+聽1
聽聽聽聽聽聽聽聽else:
聽聽聽聽聽聽聽聽聽聽聽聽return聽mid
聽聽聽聽return聽low if聽l[low]聽==聽t聽else聽False
if聽__name__聽==聽'__main__':
聽聽聽聽l聽=聽[1,聽4,聽12,聽45,聽66,聽99,聽120,聽444]
聽聽聽聽print聽binarySearch(l,聽12)
聽聽聽聽print聽binarySearch(l,聽1)
聽聽聽聽print聽binarySearch(l,聽13)
聽聽聽聽print聽binarySearch(l,聽444)
11 蹇帓
def聽qsort(seq):
聽聽聽聽if聽seq==[]:
聽聽聽聽聽聽聽聽return聽[]
聽聽聽聽else:
聽聽聽聽聽聽聽聽pivot=seq[0]
聽聽聽聽聽聽聽聽lesser=qsort([x聽for聽x聽in聽seq[1:]聽if聽x
聽聽聽聽聽聽聽聽greater=qsort([x聽for聽x聽in聽seq[1:]聽if聽x>=pivot])
聽聽聽聽聽聽聽聽return聽lesser+[pivot]+greater
if聽__name__=='__main__':
聽聽聽聽seq=[5,6,78,9,0,-1,2,3,-65,12]
聽聽聽聽print(qsort(seq))
12 鎵鹃浂闂
聽def聽聽coinChange(values,聽money,聽coinsUsed):
聽聽聽聽#values聽聽聽聽T[1:n]鏁扮粍
聽聽聽聽#valuesCounts閽卞竵瀵瑰簲鐨勭绫绘暟
聽聽聽聽#money鎵惧嚭鏉ョ殑鎬婚挶鏁�
聽聽聽聽#coinsUsed瀵瑰簲浜庣洰鍓嶉挶甯佹�绘暟i鎵�浣跨敤鐨勭‖甯佹暟鐩�
聽聽聽聽for聽cents in聽range(1,聽money+1):
聽聽聽聽聽聽聽聽minCoins聽=聽cents聽聽聽聽 #浠庣涓�涓紑濮嬪埌money鐨勬墍鏈夋儏鍐靛垵濮�
聽聽聽聽聽聽聽聽for聽value in聽values:
聽聽聽聽聽聽聽聽聽聽聽聽if聽value聽<=聽cents:
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽temp聽=聽coinsUsed[cents聽-聽value]聽+聽1
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if聽temp聽<聽minCoins:
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽minCoins聽=聽temp
聽聽聽聽聽聽聽聽coinsUsed[cents]聽=聽minCoins
聽聽聽聽聽聽聽聽print('闈㈠�间负锛歿0} 鐨勬渶灏忕‖甯佹暟鐩负锛歿1} '.format(cents,聽coinsUsed[cents])聽)
if聽__name__聽==聽'__main__':
聽聽聽聽values聽=聽[聽25,聽21,聽10,聽5,聽1]
聽聽聽聽money聽=聽63
聽聽聽聽coinsUsed聽=聽{i:0聽for聽i聽in聽range(money+1)}
聽聽聽聽coinChange(values,聽money,聽coinsUsed)
13 骞垮害閬嶅巻鍜屾繁搴﹂亶鍘嗕簩鍙夋爲
缁欏畾涓�涓暟缁勶紝鏋勫缓浜屽弶鏍戯紝骞朵笖鎸夊眰娆℃墦鍗拌繖涓簩鍙夋爲
聽## 14浜屽弶鏍戣妭鐐�
class聽Node(object):
聽聽聽聽def聽__init__(self,聽data,聽left=None,聽right=None):
聽聽聽聽聽聽聽聽self.data聽=聽data
聽聽聽聽聽聽聽聽self.left聽=聽left
聽聽聽聽聽聽聽聽self.right聽=聽right
tree聽=聽Node(1,聽Node(3,聽Node(7,聽Node(0)),聽Node(6)),聽Node(2,聽Node(5),聽Node(4)))
## 15灞傛閬嶅巻
def聽lookup(root):
聽聽聽聽stack聽=聽[root]
聽聽聽聽while聽stack:
聽聽聽聽聽聽聽聽current聽=聽stack.pop(0)
聽聽聽聽聽聽聽聽print聽current.data
聽聽聽聽聽聽聽聽if聽current.left:
聽聽聽聽聽聽聽聽聽聽聽聽stack.append(current.left)
聽聽聽聽聽聽聽聽if聽current.right:
聽聽聽聽聽聽聽聽聽聽聽聽stack.append(current.right)
## 16娣卞害閬嶅巻
def聽deep(root):
聽聽聽聽if聽not聽root:
聽聽聽聽聽聽聽聽return
聽聽聽聽print聽root.data
聽聽聽聽deep(root.left)
聽聽聽聽deep(root.right)
if聽__name__聽==聽'__main__':
聽聽聽聽lookup(tree)
聽聽聽聽deep(tree)
17 鍓嶄腑鍚庡簭閬嶅巻
娣卞害閬嶅巻鏀瑰彉椤哄簭灏監K浜�
18 姹傛渶澶ф爲娣�
def聽maxDepth(root):
聽聽聽聽聽聽聽聽if聽not聽root:
聽聽聽聽聽聽聽聽聽聽聽聽return聽0
聽聽聽聽聽聽聽聽return聽max(maxDepth(root.left),聽maxDepth(root.right))聽+聽1
19 姹備袱妫垫爲鏄惁鐩稿悓
def聽isSameTree(p,聽q):
聽聽聽聽if聽p聽==聽None聽and聽q聽==聽None:
聽聽聽聽聽聽聽聽return聽True
聽聽聽聽elif聽p聽and聽q聽:
聽聽聽聽聽聽聽聽return聽p.val聽==聽q.val and聽isSameTree(p.left,q.left)聽and聽isSameTree(p.right,q.right)
聽聽聽聽else聽:
聽聽聽聽聽聽聽聽return聽False
20 鍓嶅簭涓簭姹傚悗搴�
聽def聽rebuild(pre,聽center):
聽聽聽聽if聽not聽pre:
聽聽聽聽聽聽聽聽return
聽聽聽聽cur聽=聽Node(pre[0])
聽聽聽聽index聽=聽center.index(pre[0])
聽聽聽聽cur.left聽=聽rebuild(pre[1:index聽+聽1],聽center[:index])
聽聽聽聽cur.right聽=聽rebuild(pre[index聽+聽1:],聽center[index聽+聽1:])
聽聽聽聽return聽cur
def聽deep(root):
聽聽聽聽if聽not聽root:
聽聽聽聽聽聽聽聽return
聽聽聽聽deep(root.left)
聽聽聽聽deep(root.right)
聽聽聽聽print聽root.data
21 鍗曢摼琛ㄩ�嗙疆
聽class聽Node(object):
聽聽聽聽def聽__init__(self,聽data=None,聽next=None):
聽聽聽聽聽聽聽聽self.data聽=聽data
聽聽聽聽聽聽聽聽self.next聽=聽next
link聽=聽Node(1,聽Node(2,聽Node(3,聽Node(4,聽Node(5,聽Node(6,聽Node(7,聽Node(8,聽Node(9)))))))))
def聽rev(link):
聽聽聽聽pre聽=聽link
聽聽聽聽cur聽=聽link.next
聽聽聽聽pre.next聽=聽None
聽聽聽聽while聽cur:
聽聽聽聽聽聽聽聽tmp聽=聽cur.next
聽聽聽聽聽聽聽聽cur.next聽=聽pre
聽聽聽聽聽聽聽聽pre聽=聽cur
聽聽聽聽聽聽聽聽cur聽=聽tmp
聽聽聽聽return聽pre
root聽=聽rev(link)
while聽root:
聽聽聽聽print聽root.data
聽聽聽聽root聽=聽root.next
Python Web鐩稿叧
瑙i噴涓�涓媁SGI鍜� FastCGI 鐨勫叧绯伙紵
CGI鍏ㄧО鏄�滃叕鍏辩綉鍏虫帴鍙b��(CommonGateway Interface)锛孒TTP鏈嶅姟鍣ㄤ笌浣犵殑鎴栧叾瀹冩満鍣ㄤ笂鐨勭▼搴忚繘琛屸�滀氦璋堚�濈殑涓�绉嶅伐鍏凤紝鍏剁▼搴忛』杩愯鍦ㄧ綉缁滄湇鍔″櫒涓娿�傘��CGI鍙互鐢ㄤ换浣曚竴绉嶈瑷�缂栧啓锛屽彧瑕佽繖绉嶈瑷�鍏锋湁鏍囧噯杈撳叆銆佽緭鍑哄拰鐜鍙橀噺銆傚php,perl,tcl绛夈��
FastCGI鍍忔槸涓�涓父椹�(long-live)鍨嬬殑CGI锛屽畠鍙互涓�鐩存墽琛岀潃锛屽彧瑕佹縺娲诲悗锛屼笉浼氭瘡娆¢兘瑕佽姳璐规椂闂村幓fork涓�娆�(杩欐槸CGI鏈�涓轰汉璇熺梾鐨刦ork-and-execute妯″紡)銆傚畠杩樻敮鎸佸垎甯冨紡鐨勮繍绠�, 鍗� FastCGI 绋嬪簭鍙互鍦ㄧ綉绔欐湇鍔″櫒浠ュ鐨勪富鏈轰笂鎵ц骞朵笖鎺ュ彈鏉ヨ嚜鍏跺畠缃戠珯鏈嶅姟鍣ㄦ潵鐨勮姹傘��
FastCGI鏄瑷�鏃犲叧鐨勩�佸彲浼哥缉鏋舵瀯鐨凜GI寮�鏀炬墿灞曪紝鍏朵富瑕佽涓烘槸灏咰GI瑙i噴鍣ㄨ繘绋嬩繚鎸佸湪鍐呭瓨涓苟鍥犳鑾峰緱杈冮珮鐨勬�ц兘銆備紬鎵�鍛ㄧ煡锛孋GI瑙i噴鍣ㄧ殑鍙嶅鍔犺浇鏄疌GI鎬ц兘浣庝笅鐨勪富瑕佸師鍥狅紝濡傛灉CGI瑙i噴鍣ㄤ繚鎸佸湪鍐呭瓨涓苟鎺ュ彈FastCGI杩涚▼绠$悊鍣ㄨ皟搴︼紝鍒欏彲浠ユ彁渚涜壇濂界殑鎬ц兘銆佷几缂╂�с�丗ail- Over鐗规�х瓑绛夈��
WSGI鐨勫叏绉颁负锛歅ythonWeb Server Gateway Interface v1.0 锛圥ython Web 鏈嶅姟鍣ㄧ綉鍏虫帴鍙o級锛�
瀹冩槸Python 搴旂敤绋嬪簭鍜� WEB 鏈嶅姟鍣ㄤ箣闂寸殑涓�绉嶆帴鍙c��
瀹冪殑浣滅敤锛岀被浼间簬FCGI 鎴� FASTCGI 涔嬬被鐨勫崗璁殑浣滅敤銆�
WSGI 鐨勭洰鏍囷紝鏄寤虹珛涓�涓畝鍗曠殑鏅亶閫傜敤鐨勬湇鍔″櫒涓� WEB 妗嗘灦涔嬮棿鐨勬帴鍙c��
Flup灏辨槸浣跨敤Python 璇█瀵� WSGI 鐨勪竴绉嶅疄鐜帮紝鏄彲浠ョ敤浜� Python 鐨勫簲鐢ㄥ紑鍙戜腑鐨勪竴绉嶅伐鍏锋垨鑰呰鏄竴绉嶅簱銆�
Spawn-fcgi鏄竴涓皬绋嬪簭锛岃繖涓▼搴忕殑浣滅敤鏄鐞唂ast-cgi杩涚▼锛岄偅涔堢鐞唚sgi杩涚▼涔熸槸娌℃湁闂鐨勶紝鍔熻兘鍜宲hp-fpm绫讳技銆�
鏁咃紝绠�鍗曞湴璇达紝WSGI鍜孎astCGI閮芥槸涓�绉岰GI锛岀敤浜庤繛鎺EB鏈嶅姟鍣ㄤ笌搴旂敤绋嬪簭锛岃�學SGI涓撴寚Python搴旂敤绋嬪簭銆傝�宖lup鏄疻SGI鐨勪竴绉嶅疄鐜帮紝Spawn-fcgi鏄敤浜庣鐞唂lup杩涚▼鐨勪竴涓伐鍏凤紝鍙互鍚姩澶氫釜wsgi杩涚▼锛屽苟绠$悊瀹冧滑銆�
瑙i噴涓�涓婦jango 鍜� Tornado 鐨勫叧绯汇�佸樊鍒�
Django婧愯嚜涓�涓湪绾挎柊闂籛eb绔欑偣锛屼簬 2005 骞翠互寮�婧愮殑褰㈠紡琚噴鏀惧嚭鏉ャ��
Django 妗嗘灦鐨勬牳蹇冪粍浠舵湁锛�
鐢ㄤ簬鍒涘缓妯″瀷鐨勫璞″叧绯绘槧灏勪负鏈�缁堢敤鎴疯璁$殑瀹岀編绠$悊鐣岄潰涓�娴佺殑URL 璁捐璁捐鑰呭弸濂界殑妯℃澘璇█缂撳瓨绯荤粺绛夌瓑
瀹冮紦鍔卞揩閫熷紑鍙�,骞堕伒寰狹VC璁捐銆侱jango閬靛畧 BSD鐗堟潈锛屾渶鏂板彂琛岀増鏈槸Django
1.4锛屼簬2012骞�03鏈�23鏃ュ彂甯�.Django鐨勪富瑕佺洰鐨勬槸绠�渚裤�佸揩閫熺殑寮�鍙戞暟鎹簱椹卞姩鐨勭綉绔欍�傚畠寮鸿皟浠g爜澶嶇敤,澶氫釜缁勪欢鍙互寰堟柟渚跨殑浠モ�滄彃浠垛�濆舰寮忔湇鍔′簬鏁翠釜妗嗘灦锛孌jango鏈夎澶氬姛鑳藉己澶х殑绗笁鏂规彃浠讹紝浣犵敋鑷冲彲浠ュ緢鏂逛究鐨勫紑鍙戝嚭鑷繁鐨勫伐鍏峰寘銆傝繖浣垮緱Django鍏锋湁寰堝己鐨勫彲鎵╁睍鎬с�傚畠杩樺己璋冨揩閫熷紑鍙戝拰DRY(Do Not RepeatYourself)鍘熷垯銆�
Tornado鏄疐riendFeed浣跨敤鐨勫彲鎵╁睍鐨勯潪闃诲寮� web 鏈嶅姟鍣ㄥ強鍏剁浉鍏冲伐鍏风殑寮�婧愮増鏈�傝繖涓� Web 妗嗘灦鐪嬭捣鏉ユ湁浜涘儚 web.py 鎴栬�� Google 鐨� webapp锛屼笉杩囦负浜嗚兘鏈夋晥鍒╃敤闈為樆濉炲紡鏈嶅姟鍣ㄧ幆澧冿紝杩欎釜 Web 妗嗘灦杩樺寘鍚簡涓�浜涚浉鍏崇殑鏈夌敤宸ュ叿鍜屼紭鍖栥��
Tornado 鍜岀幇鍦ㄧ殑涓绘祦 Web 鏈嶅姟鍣ㄦ鏋讹紙鍖呮嫭澶у鏁癙ython 鐨勬鏋讹級鏈夌潃鏄庢樉鐨勫尯鍒細瀹冩槸闈為樆濉炲紡鏈嶅姟鍣紝鑰屼笖閫熷害鐩稿綋蹇�傚緱鍒╀簬鍏� 闈為樆濉炵殑鏂瑰紡鍜屽epoll鐨勮繍鐢紝Tornado 姣忕鍙互澶勭悊鏁颁互鍗冭鐨勮繛鎺ワ紝杩欐剰鍛崇潃瀵逛簬瀹炴椂 Web鏈嶅姟鏉ヨ锛孴ornado 鏄竴涓悊鎯崇殑 Web 妗嗘灦銆傛垜浠紑鍙戣繖涓� Web 鏈嶅姟鍣ㄧ殑涓昏鐩殑灏辨槸涓轰簡澶勭悊 FriendFeed 鐨勫疄鏃跺姛鑳� 鈥斺�斿湪 FriendFeed 鐨勫簲鐢ㄩ噷姣忎竴涓椿鍔ㄧ敤鎴烽兘浼氫繚鎸佺潃涓�涓湇鍔″櫒杩炴帴銆傦紙鍏充簬濡備綍鎵╁ 鏈嶅姟鍣紝浠ュ鐞嗘暟浠ュ崈璁$殑瀹㈡埛绔殑杩炴帴鐨勯棶棰樸��
瑙i噴涓媎jango-debug-toolbar鐨勪娇鐢�
浣跨敤django寮�鍙戠珯鐐规椂锛屽彲浠ヤ娇鐢╠jango-debug-toolbar鏉ヨ繘琛岃皟璇曘�傚湪settings.py涓坊鍔犫�檇ebug_toolbar.middleware.DebugToolbarMiddleware鈥欏埌椤圭洰鐨凪IDDLEWARE_CLASSES 鍐呫��
瑙i噴涓婦jango浣跨敤redis缂撳瓨鏈嶅姟鍣�
涓轰簡鑳藉湪Django涓娇鐢╮edis锛岃繕闇�瑕佸畨瑁卹edis for Django鐨勬彃浠躲�傜劧鍚庡湪Django鐨剆ettings涓厤缃簡銆傜幇鍦ㄨ繛鎺ュ拰閰嶇疆閮藉凡缁忓畬鎴愪簡锛屾帴涓嬫潵鏄竴涓畝鍗曠殑渚嬪瓙锛�
聽from django.conf import settings
from django.core.cache import cache
#read cache user id
def read_from_cache(self,聽user_name):
聽聽聽聽key聽=聽'user_id_of_'+user_name
聽聽聽聽value聽=聽cache.get(key)
聽聽聽聽if聽value聽==聽None:
聽聽聽聽聽聽聽聽data聽=聽None
聽聽聽聽else:
聽聽聽聽聽聽聽聽data聽=聽json.loads(value)
聽聽聽聽return聽data
#write cache user id
def write_to_cache(self,聽user_name):
聽聽聽聽key聽=聽'user_id_of_'+user_name
聽聽聽聽cache.set(key,聽json.dumps(user_name),聽settings.NEVER_REDIS_TIMEOUT)
濡備綍杩涜Django鍗曞厓娴嬭瘯
Django鐨勫崟鍏冩祴璇曚娇鐢╬ython鐨剈nittest妯″潡锛岃繖涓ā鍧椾娇鐢ㄥ熀浜庣被鐨勬柟娉曟潵瀹氫箟娴嬭瘯銆傜被鍚嶄负django.test.TestCase,缁ф壙浜巔ython鐨剈nittest.TestCase銆�
聽from django.test import TestCase
from myapp.models import Animal
class聽AnimalTestCase(TestCase):
聽聽聽聽def setUp(self):
聽聽聽聽聽聽聽聽Animal.objects.create(name="lion",聽sound="roar")
聽聽聽聽聽聽聽聽Animal.objects.create(name="cat",聽sound="meow")
聽聽聽聽def test_animals_can_speak(self):
聽聽聽聽聽聽聽聽"""Animals that can speak are correctly identified"""
聽聽聽聽聽聽聽聽lion聽=聽Animal.objects.get(name="lion")
聽聽聽聽聽聽聽聽cat聽=聽Animal.objects.get(name="cat")
聽聽聽聽聽聽聽聽self.assertEqual(lion.speak(),聽'The lion says "roar"')
聽聽聽聽聽聽聽聽self.assertEqual(cat.speak(),聽'The cat says "meow"')
鎵ц鐩綍涓嬫墍鏈夌殑娴嬭瘯(鎵�鏈夌殑test*.py鏂囦欢)锛氳繍琛屾祴璇曠殑鏃跺�欙紝娴嬭瘯绋嬪簭浼氬湪鎵�鏈変互test寮�澶寸殑鏂囦欢涓煡鎵炬墍鏈夌殑test cases(inittest.TestCase鐨勫瓙绫�),鑷姩寤虹珛娴嬭瘯闆嗙劧鍚庤繍琛屾祴璇曘��
1$聽python manage.py test
鎵цanimals椤圭洰涓媡ests鍖呴噷鐨勬祴璇曪細
聽$聽python manage.py testanimals.tests
鎵цanimals椤圭洰閲岀殑test娴嬭瘯锛�
1$聽python manage.py testanimals
鍗曠嫭鎵ц鏌愪釜test case锛�
1$聽python manage.py testanimals.tests.AnimalTestCase
鍗曠嫭鎵ц鏌愪釜娴嬭瘯鏂规硶锛�
1$聽python manage.py testanimals.tests.AnimalTestCase.test_animals_can_speak
涓烘祴璇曟枃浠舵彁渚涜矾寰勶細
1$聽python manage.py testanimals/
閫氶厤娴嬭瘯鏂囦欢鍚嶏細
1$聽python manage.py test--pattern="tests_*.py"
鍚敤warnings鎻愰啋锛�
1$聽python聽-Wall manage.py test
瑙i噴涓婬ttp鍗忚
HTTP鏄竴涓睘浜庡簲鐢ㄥ眰鐨勯潰鍚戝璞$殑鍗忚锛岀敱浜庡叾绠�鎹枫�佸揩閫熺殑鏂瑰紡锛岄�傜敤浜庡垎甯冨紡瓒呭獟浣撲俊鎭郴缁熴��
HTTP鍗忚鐨勪富瑕佺壒鐐瑰彲姒傛嫭濡備笅锛�
1.鏀寔瀹㈡埛/鏈嶅姟鍣ㄦā寮忋��
2.绠�鍗曞揩閫燂細瀹㈡埛鍚戞湇鍔″櫒璇锋眰鏈嶅姟鏃讹紝鍙渶浼犻�佽姹傛柟娉曞拰璺緞銆傝姹傛柟娉曞父鐢ㄧ殑鏈塆ET銆丠EAD銆丳OST銆傛瘡绉嶆柟娉曡瀹氫簡瀹㈡埛涓庢湇鍔″櫒鑱旂郴鐨勭被鍨嬩笉鍚屻�傜敱浜嶩TTP鍗忚绠�鍗曪紝浣垮緱HTTP鏈嶅姟鍣ㄧ殑绋嬪簭瑙勬ā灏忥紝鍥犺�岄�氫俊閫熷害寰堝揩銆�
3.鐏垫椿锛欻TTP鍏佽浼犺緭浠绘剰绫诲瀷鐨勬暟鎹璞°�傛鍦ㄤ紶杈撶殑绫诲瀷鐢盋ontent-Type鍔犱互鏍囪銆�
4.鏃犺繛鎺ワ細鏃犺繛鎺ョ殑鍚箟鏄檺鍒舵瘡娆¤繛鎺ュ彧澶勭悊涓�涓姹傘�傛湇鍔″櫒澶勭悊瀹屽鎴风殑璇锋眰锛屽苟鏀跺埌瀹㈡埛鐨勫簲绛斿悗锛屽嵆鏂紑杩炴帴銆傞噰鐢ㄨ繖绉嶆柟寮忓彲浠ヨ妭鐪佷紶杈撴椂闂淬��
5.鏃犵姸鎬侊細HTTP鍗忚鏄棤鐘舵�佸崗璁�傛棤鐘舵�佹槸鎸囧崗璁浜庝簨鍔″鐞嗘病鏈夎蹇嗚兘鍔涖�傜己灏戠姸鎬佹剰鍛崇潃濡傛灉鍚庣画澶勭悊闇�瑕佸墠闈㈢殑淇℃伅锛屽垯瀹冨繀椤婚噸浼狅紝杩欐牱鍙兘瀵艰嚧姣忔杩炴帴浼犻�佺殑鏁版嵁閲忓澶с�傚彟涓�鏂归潰锛屽湪鏈嶅姟鍣ㄤ笉闇�瑕佸厛鍓嶄俊鎭椂瀹冪殑搴旂瓟灏辫緝蹇��
瑙i噴涓婬ttp璇锋眰澶村拰甯歌鍝嶅簲鐘舵�佺爜
Accept:鎸囨祻瑙堝櫒鎴栧叾浠栧鎴峰彲浠ユ帴鐖辩殑MIME鏂囦欢鏍煎紡銆傚彲浠ユ牴鎹畠鍒ゆ柇骞惰繑鍥為�傚綋鐨勬枃浠舵牸寮忋��
Accept-Charset锛氭寚鍑烘祻瑙堝櫒鍙互鎺ュ彈鐨勫瓧绗︾紪鐮併�傝嫳鏂囨祻瑙堝櫒鐨勯粯璁ゅ�兼槸ISO-8859-1.
Accept-Language锛氭寚鍑烘祻瑙堝櫒鍙互鎺ュ彈鐨勮瑷�绉嶇被锛屽en鎴杄n-us锛屾寚鑻辫銆�
Accept-Encoding锛氭寚鍑烘祻瑙堝櫒鍙互鎺ュ彈鐨勭紪鐮佹柟寮忋�傜紪鐮佹柟寮忎笉鍚屼簬鏂囦欢鏍煎紡锛屽畠鏄负浜嗗帇缂╂枃浠跺苟鍔犻�熸枃浠朵紶閫掗�熷害銆傛祻瑙堝櫒鍦ㄦ帴鏀跺埌Web鍝嶅簲涔嬪悗鍏堣В鐮侊紝鐒跺悗鍐嶆鏌ユ枃浠舵牸寮忋��
Cache-Control锛氳缃叧浜庤姹傝浠g悊鏈嶅姟鍣ㄥ瓨鍌ㄧ殑鐩稿叧閫夐」銆備竴鑸敤涓嶅埌銆�
Connection锛氱敤鏉ュ憡璇夋湇鍔″櫒鏄惁鍙互缁存寔鍥哄畾鐨凥TTP杩炴帴銆侶TTP/1.1浣跨敤Keep-Alive涓洪粯璁ゅ�硷紝杩欐牱锛屽綋娴忚鍣ㄩ渶瑕佸涓枃浠舵椂(姣斿涓�涓狧TML鏂囦欢鍜岀浉鍏崇殑鍥惧舰鏂囦欢)锛屼笉闇�瑕佹瘡娆¢兘寤虹珛杩炴帴銆�
Content-Type锛氱敤鏉ヨ〃鍚峳equest鐨勫唴瀹圭被鍨嬨�傚彲浠ョ敤HttpServletRequest鐨刧etContentType()鏂规硶鍙栧緱銆�
Cookie锛氭祻瑙堝櫒鐢ㄨ繖涓睘鎬у悜鏈嶅姟鍣ㄥ彂閫丆ookie銆侰ookie鏄湪娴忚鍣ㄤ腑瀵勫瓨鐨勫皬鍨嬫暟鎹綋锛屽畠鍙互璁拌浇鍜屾湇鍔″櫒鐩稿叧鐨勭敤鎴蜂俊鎭紝涔熷彲浠ョ敤鏉ュ疄鐜颁細璇濆姛鑳姐��
鐘舵�佷唬鐮佹湁涓変綅鏁板瓧缁勬垚锛岀涓�涓暟瀛楀畾涔変簡鍝嶅簲鐨勭被鍒紝涓旀湁浜旂鍙兘鍙栧�硷細
1xx锛氭寚绀轰俊鎭�撹〃绀鸿姹傚凡鎺ユ敹锛岀户缁鐞�
2xx锛氭垚鍔熲�撹〃绀鸿姹傚凡琚垚鍔熸帴鏀躲�佺悊瑙c�佹帴鍙�
3xx锛氶噸瀹氬悜鈥撹瀹屾垚璇锋眰蹇呴』杩涜鏇磋繘涓�姝ョ殑鎿嶄綔
4xx锛氬鎴风閿欒鈥撹姹傛湁璇硶閿欒鎴栬姹傛棤娉曞疄鐜�
5xx锛氭湇鍔″櫒绔敊璇�撴湇鍔″櫒鏈兘瀹炵幇鍚堟硶鐨勮姹�
甯歌鐘舵�佷唬鐮併�佺姸鎬佹弿杩般�佽鏄庯細
200 OK聽聽聽聽聽//瀹㈡埛绔姹傛垚鍔�
400 Bad Request聽 //瀹㈡埛绔姹傛湁璇硶閿欒锛屼笉鑳借鏈嶅姟鍣ㄦ墍鐞嗚В
401 Unauthorized //璇锋眰鏈粡鎺堟潈锛岃繖涓姸鎬佷唬鐮佸繀椤诲拰WWW-Authenticate鎶ュご鍩熶竴璧蜂娇鐢�
403 Forbidden聽 //鏈嶅姟鍣ㄦ敹鍒拌姹傦紝浣嗘槸鎷掔粷鎻愪緵鏈嶅姟
404 Not Found聽 //璇锋眰璧勬簮涓嶅瓨鍦紝eg锛氳緭鍏ヤ簡閿欒鐨刄RL
500 Internal Server Error //鏈嶅姟鍣ㄥ彂鐢熶笉鍙鏈熺殑閿欒
503 Server Unavailable聽 //鏈嶅姟鍣ㄥ綋鍓嶄笉鑳藉鐞嗗鎴风鐨勮姹傦紝涓�娈垫椂闂村悗鍙兘鎭㈠姝e父
eg锛欻TTP/1.1 200 OK 锛圕RLF锛�
鐖櫕
涓�銆佽瘯鍒楀嚭鑷冲皯涓夌鐩墠娴佽鐨勫ぇ鍨嬫暟鎹簱鐨勫悕绉�:________銆乢________銆乢_________,鍏朵腑鎮ㄦ渶鐔熸倝鐨勬槸__________,浠巁_________骞村紑濮嬩娇鐢ㄣ��
Oracle锛孧ysql锛孲QLServer Oracle鏍规嵁鑷繁鎯呭喌
浜屻�佹湁琛↙ist锛屽苟鏈夊瓧娈礎銆丅銆丆锛岀被鍨嬮兘鏄暣鏁般�傝〃涓湁濡備笅鍑犳潯璁板綍锛�
ABC
279
564
3119
鐜板湪瀵硅琛ㄤ竴娆″畬鎴愪互涓嬫搷浣滐細
鏌ヨ鍑築鍜孋鍒楃殑鍊硷紝瑕佹眰鎸塀鍒楀崌搴忔帓鍒�
鍐欏嚭涓�鏉℃柊鐨勮褰曪紝鍊间负{7,9,8}
鏌ヨC鍒楋紝瑕佹眰娑堥櫎閲嶅鐨勫�硷紝鎸夐檷搴忔帓鍒�
鍐欏嚭瀹屾垚瀹屾垚浠ヤ笂鎿嶄綔鐨勬爣鍑嗙殑SQL璇彞锛屽苟涓斿啓鍑烘搷浣�3鐨勭粨鏋溿��
create table List(A int,B int,C int)
Select B,C from List order by B
Insert into List values(7,9,8)
Select distinct(C) from List order by desc;
984
涓夈�佽绠�瑕佽鏄庤鍥剧殑浣滅敤
1.鏁版嵁搴撹鍥鹃殣钘忎簡鏁版嵁鐨勫鏉傛�с��
2.鏁版嵁搴撹鍥炬湁鍒╀簬鎺у埗鐢ㄦ埛瀵硅〃涓煇浜涘垪鐨勮闂��
3.鏁版嵁搴撹鍥句娇鐢ㄦ埛鏌ヨ鍙樺緱绠�鍗曘��
鍥涖�佸垪涓炬偍浣跨敤杩囩殑python缃戠粶鐖櫕鎵�鐢ㄥ埌鐨勭綉缁滄暟鎹寘锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�
requests銆乽rllib銆乽rllib2銆乭ttplib2
浜斻�佸垪涓炬偍浣跨敤杩囩殑python缃戠粶鐖櫕鎵�鐢ㄥ埌鐨勮В鏋愭暟鎹寘锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�
BeautifulSoup銆乸yquery銆乆path銆乴xml
鍏�佸垪涓炬偍浣跨敤杩囩殑python涓殑缂栫爜鏂瑰紡锛堟渶鐔熸倝鐨勫湪鍓嶏級锛�
UTF-8锛孉SCII锛実bk
[if !supportLists]聽[endif]
涓冦�乸ython3.5璇█涓璭numerate鐨勬剰鎬濇槸_______________________
瀵逛簬涓�涓彲杩唬鐨勶紙iterable锛�/鍙亶鍘嗙殑瀵硅薄锛堝鍒楄〃銆佸瓧绗︿覆锛夛紝enumerate灏嗗叾缁勬垚涓�涓储寮曞簭鍒楋紝鍒╃敤瀹冨彲浠ュ悓鏃惰幏寰楃储寮曞拰鍊�
enumerate澶氱敤浜庡湪for寰幆涓緱鍒拌鏁�
鍏��99鐨勫叓杩涘埗琛ㄧず鏄痏______________________
143
涔濄�佽涓惧嚭涓夌甯哥敤鐨勬帓搴忕畻娉�
鍐掓场銆侀�夋嫨銆佸揩閫�
鍗併�佸垪鍑烘瘮杈冪啛鎮夌殑鐖櫕妗嗘灦
Scrapy
鍗佷竴銆佺敤4銆�9銆�2銆�7鍥涗釜鏁板瓧锛屽彲浠ヤ娇鐢�+銆�-銆�*鍜�/锛屾瘡涓暟瀛椾娇鐢ㄤ竴娆★紝浣胯〃杈惧紡鐨勭粨鏋滀负24锛岃〃杈惧紡鏄痏____________________________
锛�9+7-4锛�*2
鍗佷簩銆佸浣犳渶鏈夊奖鍝嶇殑鎴栨槸鎮ㄨ涓烘渶鏈変环鍊肩殑杞欢鏂归潰鐨勫嚑鏈功鏄紵
鍗佷笁銆佹偍鏈�鐔熸倝鐨刄nix鐜鏄痏____________.Unix涓嬫煡璇㈢幆澧冨彉閲忕殑鍛戒护鏄痏_______,鏌ヨ鑴氭湰瀹氭椂浠诲姟鐨勫懡浠ゆ槸____________________
1AIX锛宔nvcrontab
鍗佸洓銆佸啓鍑哄湪缃戠粶鐖櫕鐖彇鏁版嵁鐨勮繃绋嬩腑锛岄亣鍒扮殑闃茬埇铏棶棰樼殑瑙e喅鏂规
閫氳繃headers鍙嶇埇铏細瑙e喅绛栫暐锛屼吉閫爃eaders
鍩轰簬鐢ㄦ埛琛屼负鍙嶇埇铏細鍔ㄦ�佸彉鍖栧幓鐖彇鏁版嵁锛屾ā鎷熸櫘閫氱敤鎴风殑琛屼负
鍩轰簬鍔ㄦ�侀〉闈㈢殑鍙嶇埇铏細璺熻釜鏈嶅姟鍣ㄥ彂閫佺殑ajax璇锋眰锛屾ā鎷焌jax璇锋眰
鍗佷簲銆侀槄璇讳互涓婸ython绋嬪簭
foriinrange(5,0,-1):
print(i)
璇峰湪涓嬮潰鍐欏嚭鎵撳嵃缁撴灉
54321
鍗佸叚銆佸湪鏌愮郴缁熶腑涓�涓暣鏁板崰鐢ㄤ袱涓叓浣嶅瓧鑺傦紝浣跨敤Python鎸変笅闈㈢殑瑕佹眰缂栧啓瀹屾暣绋嬪簭銆�
鎺ユ敹浠庢爣鍑嗚緭鍏ヤ腑渚濇杈撳叆鐨勪簲涓暟瀛楋紝灏嗗叾缁勫悎鎴愪负涓�涓暣鏁帮紝鏀惧叆鍏ㄥ眬鍙橀噺n涓紝闅忓悗鍦ㄦ爣鍑嗚緭鍑鸿緭鍑鸿繖涓暣鏁般�傦紙ord(char)鑾峰彇瀛楃ASCII鍊肩殑鍑芥暟锛�
浜猴紝浠庡垰鍑虹敓鏉ュ埌杩欎釜涓栫晫锛屼究寮�濮嬫帰绱㈣繖涓笘鐣屻�傜疮浜嗗氨姝囦細锛岀簿绁炰簡灏辩户缁帰绱紝鐩磋嚦姝讳骸銆�