2018-06-22

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鍊肩殑鍑芥暟锛�


浜猴紝浠庡垰鍑虹敓鏉ュ埌杩欎釜涓栫晫锛屼究寮�濮嬫帰绱㈣繖涓笘鐣屻�傜疮浜嗗氨姝囦細锛岀簿绁炰簡灏辩户缁帰绱紝鐩磋嚦姝讳骸銆�

你可能感兴趣的:(2018-06-22)