锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品99久久久久久久久,欧美在线亚洲一区,亚洲少妇在线http://m.shnenglu.com/Jeff-Chen/category/1412.htmlzh-cnMon, 26 May 2008 12:07:21 GMTMon, 26 May 2008 12:07:21 GMT60python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ,綰㈠瓧閮ㄥ垎涓虹粡鍏歌鏄庯級http://m.shnenglu.com/Jeff-Chen/archive/2006/04/17/5785.htmlJeff-ChenJeff-ChenMon, 17 Apr 2006 11:27:00 GMThttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/17/5785.htmlhttp://m.shnenglu.com/Jeff-Chen/comments/5785.htmlhttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/17/5785.html#Feedback0http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5785.htmlhttp://m.shnenglu.com/Jeff-Chen/services/trackbacks/5785.html

python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ錛?/a>

i = 1

聽 榪欐槸涓涓啀綆鍗曚笉榪囩殑璧嬪艱鍙ワ紝鍗充究鏄墠寮濮嬪涔犵紪紼嬬殑鏂版墜涔熻兘鑴卞彛鑰屽嚭瀹冪殑鍚箟 -- 鈥滆緗彉閲廼鐨勫間負1鈥濄?/p>

i = 2

聽 鈥滃皢鍙橀噺i鐨勫兼敼涓?鈥濓紝褰撶湅鍒版帴涓嬫潵榪欒浠g爜鏃訛紝浣犺剳嫻蜂腑鑲畾浼氱珛鍗蟲誕鐜拌繖鏍風殑蹇靛ご銆?/p>

聽 榪欓毦閬撲細鏈夐棶棰樺槢錛熻繖綆綆鍗曞崟鐨勪竴琛岃祴鍊艱鍙ュ叾瀹炲寘鍚簡python涓殑涓変釜閲嶈姒傚康錛氬悕瀛椼佺粦瀹氬拰瀵硅薄銆?br />python瀵硅祴鍊艱鍙ヤ綔鍑轟簡鑷繁鐨勫畾涔夛細
聽 鈥滅鍊艱鍙ユ槸鐢ㄦ潵灝嗗悕瀛楃粦瀹氾紙鎴栭噸鏂扮粦瀹氾級鍒版煇涓璞$殑鎿嶄綔錛岃屼笖瀹冧篃鍙敤鏉ヤ慨鏀瑰彲鍙樺璞$殑灞炴ф垨
瀵硅薄涓墍鍖呭惈鐨勬垚鍛樸傗?/p>

聽 鍚嶅瓧緇戝畾鍒板璞¤繖涓蹇靛湪python涓殢澶勫彲瑙侊紝鍙互璇存槸python鐨勬渶鍩烘湰鑰屼笖鏈閲嶈鐨勬蹇典箣涓銆傚鏋?br />娌℃湁寰堝ソ鐞嗚В榪欎竴鐐癸紝涓浜涙剰鎯充笉鍒扮殑緇撴灉灝變細鍦ㄦ偍鐨勪唬鐮佷腑鎮勭劧鍑虹幇銆?/p>

聽 鍏堟潵鐪嬩竴涓畝鍗曚緥瀛愶細

>>> a = {'g':1}
>>> b = a*4
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b

聽 鍑轟箮鎰忔枡鍢涳紵璇鋒參鎱㈢湅瀹岃繖綃囨枃绔犮?/p>

1. 瀵硅薄
聽 鈥滀竾鐗╃殕瀵硅薄鈥?Everything is object)錛岃繖鏄痯ython榪欑闈㈠悜瀵硅薄璇█鎵鍊″鐨勭悊蹇點傚湪鎴戜滑鐔熸倝鐨凜++涓紝1鍙槸涓涓暣鍨嬫暟錛岃屼笉鏄竴涓璞°備絾鍦╬ython涓紝1鍗存槸涓涓疄瀹炲湪鍦ㄧ殑瀵硅薄錛屾偍鍙互鐢╠ir(1)鏉ユ樉紺哄畠鐨勫睘鎬с?/p>

聽 鍦╬ython涓紝鎵鏈夊璞¢兘鏈変笅闈笁涓壒寰侊細
聽* 鍞竴鐨勬爣璇嗙爜(identity)
聽* 綾誨瀷
聽* 鍐呭錛堟垨縐頒負鍊鹼級

聽 涓鏃﹀璞¤鍒涘緩錛屽畠鐨勬爣璇嗙爜灝變笉鍏佽鏇存敼銆傚璞$殑鏍囪瘑鐮佸彲浠ユ湁鍐呭緩鍑芥暟id()鑾峰彇錛屽畠鏄竴涓暣鍨嬫暟銆傛偍鍙互灝嗗畠鎯寵薄涓鴻瀵硅薄鍦ㄥ唴瀛樹腑鐨勫湴鍧錛屽叾瀹炲湪鐩墠鐨勫疄鐜頒腑鏍囪瘑鐮佷篃灝辨槸璇ュ璞$殑鍐呭瓨鍦板潃銆?/p>

>>> class c1:
聽pass
...
>>> obj = c1()
>>> obj
<__main__.c1 instance at 0x00AC0738>
>>> id(obj)
11274040

聽 鎹㈢畻涓涓嬶紝11274040灝辨槸鍗佸叚榪涘埗鐨?x00AC0738銆?/p>

>>> id(1)
7957136

聽 榪欏氨鏄墠闈㈡彁鍒扮殑1榪欎釜瀵硅薄鐨勬爣璇嗙爜錛屼篃灝辨槸瀹冨湪鍐呭瓨涓殑鍦板潃銆?/font>

聽 褰撶敤is鎿嶄綔絎︽瘮杈冧袱涓璞℃椂錛屽氨鏄湪姣旇緝瀹冧滑鐨勬爣璇嗙爜銆傛洿紜垏鍦拌錛宨s鎿嶄綔絎︽槸鍦ㄥ垽鏂袱涓璞℃槸鍚︽槸鍚屼竴涓璞°?br />>>> [1] is [1]
聽 鍏剁粨鏋滄槸False錛屾槸鍥犱負榪欐槸涓や釜涓嶅悓鐨勫璞★紝瀛樺偍鍦ㄥ唴瀛樹腑鐨勪笉鍚屽湴鏂廣?/font>

>>> [1] == [1]
聽 鍏剁粨鏋滄槸True錛屾槸鍥犱負榪欎袱涓笉鍚岀殑瀵硅薄鏈夌潃鐩稿悓鐨勫箋?/font>

聽 涓庡璞$殑鏍囪瘑鐮佺被浼鹼紝瀵硅薄鐨勭被鍨嬩篃鏄笉鍙洿鏀圭殑銆傚彲浠ョ敤鍐呭緩鍑芥暟type()鍙栧緱瀵硅薄鐨勭被鍨嬨?/p>

聽 鏈夌殑瀵硅薄鐨勫兼槸鍙互鏀瑰彉鐨勶紝榪欑被瀵硅薄鍙綔鍙彉瀵硅薄錛涜屽彟澶栦竴浜涘璞″湪鍒涘緩鍚庡叾鍊兼槸涓嶅彲鏀瑰彉鐨勶紙濡?榪欎釜瀵硅薄錛夛紝榪欑被瀵硅薄鍙綔鎭掑畾瀵硅薄銆傚璞$殑鍙彉鎬ф槸鐢卞畠鐨勭被鍨嬪喅瀹氱殑錛屾瘮濡傛暟鍊煎瀷(number)銆佸瓧絎︿覆鍨?string)浠ュ強搴忓垪鍨?tuple)鐨勫璞℃槸鎭掑畾瀵硅薄錛涜屽瓧鍏稿瀷(dictionary)鍜屽垪琛ㄥ瀷(list)鐨勫璞℃槸鍙彉瀵硅薄銆?/p>

聽 闄や簡涓婇潰鎻愬埌鐨勪笁涓壒寰佸錛屼竴涓璞″彲鑳斤細
聽* 娌℃湁鎴栬呮嫢鏈夊涓柟娉?br />聽* 娌℃湁鎴栬呮湁澶氫釜鍚嶅瓧

2. 鍚嶅瓧
聽 鍚嶅瓧鏄涓涓璞$殑縐板懠錛屼竴涓璞″彲浠ュ彧鏈変竴涓悕瀛楋紝涔熷彲浠ユ病鏈夊悕瀛楁垨鍙栧涓悕瀛椼備絾瀵硅薄鑷繁鍗翠笉鐭ラ亾鏈夊灝戝悕瀛楋紝鍙粈涔堬紝鍙湁鍚嶅瓧鏈韓鐭ラ亾瀹冩墍鎸囧悜鐨勬槸涓粈涔堝璞°傜粰瀵硅薄鍙栦竴涓悕瀛楃殑鎿嶄綔鍙綔鍛藉悕錛宲ython灝嗚祴鍊艱鍙ヨ涓烘槸涓涓懡鍚嶆搷浣滐紙鎴栬呯О涓哄悕瀛楃粦瀹氾級銆?/p>

聽 鍚嶅瓧鍦ㄤ竴瀹氱殑鍚嶅瓧絀洪棿鍐呮湁鏁堬紝鑰屼笖鍞竴錛屼笉鍙兘鍦ㄥ悓涓涓悕瀛楃┖闂村唴鏈変袱涓垨鏇村鐨勫璞″彇鍚屼竴鍚嶅瓧銆?/p>

聽 璁╂垜浠啀鏉ョ湅鐪嬫湰綃囩殑絎竴涓緥瀛愶細i = 1銆傚湪python涓紝瀹冩湁濡備笅涓や釜鍚箟錛?br />聽* 鍒涘緩涓涓間負1鐨勬暣鍨嬪璞?br />聽* "i"鏄寚鍚戣鏁村瀷瀵硅薄鐨勫悕瀛楋紙鑰屼笖瀹冩槸涓涓紩鐢級

3. 緇戝畾
聽 濡備笂鎵璁茬殑錛岀粦瀹氬氨鏄皢涓涓璞′笌涓涓悕瀛楄仈緋昏搗鏉ャ傛洿紜垏鍦拌錛屽氨鏄鍔犺瀵硅薄鐨勫紩鐢ㄨ鏁般備紬鎵鍛ㄧ煡錛孋++涓竴澶ч棶棰樺氨鏄唴瀛樻硠婕?-- 鍗沖姩鎬佸垎閰嶇殑鍐呭瓨娌℃湁鑳藉鍥炴敹錛岃岃В鍐寵繖涓闂鐨勫埄鍣ㄤ箣涓灝辨槸寮曠敤璁℃暟銆俻ython灝遍噰鐢ㄤ簡榪欎竴鎶鏈疄鐜板叾鍨冨溇鍥炴敹鏈哄埗銆?br />聽
聽 python涓殑鎵鏈夊璞¢兘鏈夊紩鐢ㄨ鏁般?/p>

i=i+1

* 榪欏垱寤轟簡涓涓柊鐨勫璞★紝鍏跺間負i+1銆?br />* "i"榪欎釜鍚嶅瓧鎸囧悜浜嗚鏂板緩鐨勫璞★紝璇ュ璞$殑寮曠敤璁℃暟鍔犱竴錛岃?i"浠ュ墠鎵鎸囧悜鐨勮佸璞$殑
聽 寮曠敤璁℃暟鍑忎竴銆?br />* "i"鎵鎸囧悜鐨勮佸璞$殑鍊煎茍娌℃湁鏀瑰彉銆?br />* 榪欏氨鏄負浠涔堝湪python涓病鏈?+銆?-榪欐牱鐨勫崟鐩繍綆楃鐨勪竴涓師鍥犮?/p>

3.1 寮曠敤璁℃暟
聽 瀵硅薄鐨勫紩鐢ㄨ鏁板湪涓嬪垪鎯呭喌涓嬩細澧炲姞錛?br />聽* 璧嬪兼搷浣?br />聽* 鍦ㄤ竴涓鍣紙鍒楄〃錛屽簭鍒楋紝瀛楀吀絳夌瓑錛変腑鍖呭惈璇ュ璞?/p>

聽 瀵硅薄鐨勫紩鐢ㄨ鏁板湪涓嬪垪鎯呭喌涓嬩細鍑忓皯錛?br />聽* 紱誨紑浜嗗綋鍓嶇殑鍚嶅瓧絀洪棿錛堣鍚嶅瓧絀洪棿涓殑鏈湴鍚嶅瓧閮戒細琚攢姣侊級
聽* 瀵硅薄鐨勪竴涓悕瀛楄緇戝畾鍒板彟澶栦竴涓璞?br />聽* 瀵硅薄浠庡寘鍚畠鐨勫鍣ㄤ腑縐婚櫎
聽* 鍚嶅瓧琚樉紺哄湴鐢╠el閿姣侊紙濡傦細del i錛?/p>

聽 褰撳璞$殑寮曠敤璁℃暟闄嶅埌0鍚庯紝璇ュ璞″氨浼氳閿姣侊紝鍏舵墍鍗犵殑鍐呭瓨涔熷氨寰椾互鍥炴敹銆?/p>

4. 鍚嶅瓧緇戝畾鎵甯︽潵鐨勪竴浜涘鐗圭幇璞?/p>

渚?.1錛?br />>>> li1 = [7, 8, 9, 10]
>>> li2 = li1
>>> li1[1] = 16
>>> print li2
[7, 16, 9, 10]

娉ㄨВ錛氳繖閲宭i1涓巐i2閮芥寚鍚戝悓涓涓垪琛ㄥ璞7, 8, 9, 10]錛屸渓i[1] = 16鈥濇槸鏀瑰彉璇ュ垪琛ㄤ腑鐨勭2涓厓绱狅紝鎵浠ラ氳繃li2鏃跺悓鏍蜂細鐪嬪埌榪欎竴鏀瑰姩銆?/p>

渚?.2錛?br />>>> b = [{'g':1}]*4
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]

渚?.3錛?br /> >>> b = [{'g':1}] + [{'g':1}] + [{'g':1}] + [{'g':1}]
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 1}, {'g': 1}, {'g': 1}]

娉ㄨВ錛氬湪鏈夌殑python涔︿腑璁插埌涔樻硶絎﹀彿錛?錛夊氨鐩稿綋浜庡嚑涓姞娉曠殑閲嶅錛屽嵆璁や負渚?.2搴旇涓?.3鐨勭粨鏋滀竴鑷淬?br />聽聽聽聽聽 鍏跺疄涓嶇劧銆備緥4.2涓殑b榪欎釜鍒楄〃涓殑姣忎竴涓厓绱爗'g': 1}鍏跺疄閮芥槸鍚屼竴涓璞★紝鍙互鐢╥d(b[n])榪涜楠岃瘉銆傝屼緥4.3涓垯鏄洓涓笉鍚岀殑瀵硅薄銆傛垜浠彲浠ラ噰鐢ㄥ悕瀛楃粦瀹氱殑鏂規硶娑堥櫎榪欎竴姝т箟錛?/font>

>>> a = {'g' : 1}
>>> b = [a]*4
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]
>>> print a
{'g': 2}

>>> a = {'g' : 1}
>>> b = [a] + [a] + [a] + [a]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]
>>> print a
{'g': 2}

聽 涓嶈繃瀵逛簬鎭掑畾瀵硅薄鑰岃█錛屸?鈥濆拰榪炵畫鍔犳硶鐨勬晥鏋滀竴鏍楓傛瘮濡傦紝b=[1] * 4 灝辯瓑鍚屼簬 b=[1]+[1]+[1]+[1]銆?/p>

5. 鍑芥暟鐨勪紶鍙傞棶棰?br />聽 鍑芥暟鐨勫弬鏁頒紶閫掍篃鏄竴涓悕瀛椾笌瀵硅薄鐨勭粦瀹氳繃紼嬶紝鑰屼笖鏄粦瀹氬埌鍙﹀涓涓悕瀛楃┖闂達紙鍗沖嚱鏁頒綋鍐呴儴鐨勫悕瀛楃┖闂達級銆俻ython瀵硅祴鍊艱鍙ョ殑鐙壒鐪嬫硶鍙堜細瀵瑰嚱鏁扮殑浼犻掗犳垚浠涔堝獎鍝嶅憿錛?/p>

5.1 浼犲鹼紵浼犲潃錛?br />聽 鍦ㄥ涔燙++鐨勬椂鍊欐垜浠兘鐭ラ亾鏈変袱縐嶅弬鏁頒紶閫掓柟寮忥細浼犲煎拰浼犲潃銆傝屽湪python涓墍鏈夌殑鍙傛暟浼犻掗兘鏄紩鐢ㄤ紶閫掞紙pass reference錛夛紝涔熷氨鏄紶鍧銆傝繖鏄敱浜庡悕瀛楁槸瀵硅薄鐨勪竴涓紩鐢ㄨ繖涓python鐨勭壒鎬ц岃嚜鐒跺緱鏉ョ殑錛屽湪鍑芥暟浣撳唴閮ㄥ鏌愪竴澶栭儴鍙彉瀵硅薄浣滀簡淇敼鑲畾浼氬皢鍏舵敼鍙樺甫鍒板嚱鏁頒互澶栥傝鎴戜滑鏉ョ湅鐪嬩笅闈?br />榪欎釜渚嬪瓙錛?/p>

渚?.1
>>> a = [1, 2, 3]
>>> def foo(par):
...聽par[1] = 10
...
>>> foo(a)
>>> print a
[1, 10, 3]

聽 鍥犳錛屽湪python涓紝鎴戜滑搴旇鎶涘紑浼犻掑弬鏁拌繖縐嶆蹇碉紝鏃跺埢鐗㈣鍑芥暟鐨勮皟鐢ㄥ弬鏁版槸灝嗗璞$敤鍙﹀涓涓悕瀛楃┖闂寸殑鍚嶅瓧緇戝畾銆傚湪鍑芥暟涓紝涓嶈繃鏄敤浜嗗彟澶栦竴涓悕瀛楋紝浣嗚繕鏄榪欏悓涓涓璞¤繘琛屾搷浣溿?/p>

5.2 緙虹渷鍙傛暟
聽 浣跨敤緙虹渷鍙傛暟錛屾槸鎴戜滑鍠滅埍鐨勪竴縐嶄綔娉曘傝繖鍙互鍦ㄨ皟鐢ㄨ鍑芥暟鏃惰妭鐪佷笉灝戠殑鍑婚敭嬈℃暟錛岃屼笖浠g爜涔熸樉寰楁洿鍔犵畝媧併傛洿閲嶈鐨勬槸瀹冧粠鏌愮鎰忎箟涓婁綋鐜頒簡榪欎釜鍑芥暟璁捐鐨勫垵琛楓?br />聽 浣嗘槸python涓殑緙虹渷鍙傛暟錛屽嵈闅愯棌鐫涓涓巹鏈猴紝鍒濆鑰呰偗瀹氫細鍦ㄤ笂闈㈡牻璺熷ご錛岃屼笖榪欎釜閿欒闈炲父闅愮銆傚厛鐪嬬湅涓嬮潰榪欎釜渚嬪瓙錛?/p>

渚?.2
>>> def foo(par=[]):
...聽par.append(0)
...聽print par
...聽
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎竴嬈¤皟鐢?br />[0]
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎簩嬈¤皟鐢?br />[0, 0]

聽 鍑轟簡浠涔堥棶棰橈紵榪欎釜鍙傛暟par濂藉儚綾諱技涓嶤涓殑闈欐佸彉閲忥紝绱浜嗕互鍓嶇殑緇撴灉銆傛槸榪欐牱鍚楋紵褰撶劧涓嶆槸錛岃繖閮芥槸鈥滃璞°佸悕瀛椼佺粦瀹氣濊繖浜涙濇兂鎯圭殑鈥滅ジ鈥濄傗滀竾鐗╃殕瀵硅薄鈥濓紝榪樿寰楀悧錛熻繖閲岋紝鍑芥暟foo褰撶劧涔熸槸涓涓璞★紝鍙互縐頒箣涓哄嚱鏁板璞★紙涓庝竴鑸殑瀵硅薄娌′粈涔堜笉鍚岋級銆傚厛鏉ョ湅鐪嬭繖涓璞℃湁浜涗粈涔堝睘鎬с?/p>

>>> dir(foo)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

聽 鍗曚粠鍚嶅瓧涓婄湅錛屸渇unc_defaults鈥濆緢鍙兘涓庣己鐪佸弬鏁版湁鍏籌紝鐪嬬湅瀹冪殑鍊箋?/p>

>>> foo.func_defaults聽聽聽聽聽聽聽聽聽 # 鏄劇ず榪欎釜灞炴х殑鍐呭
([0, 0],)
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎笁嬈¤皟鐢?br />[0, 0, 0]
>>> foo.func_defaults聽聽聽聽聽聽聽聽聽 # 鍐嶆潵鐪嬬湅榪欎釜灞炴?br />([0, 0, 0],)

聽 鏋滀笉鍏剁劧錛屽氨鏄繖涓簭鍒楀璞★紙tuple)鍖呭惈浜嗘墍鏈夌殑緙虹渷鍙傛暟銆傞獙璇佷竴涓嬶細

>>> def fooM(par1, def1=1, def2=[], def3='str'):聽聽聽聽聽聽聽聽聽聽 # 瀹氫箟涓涓湁澶氫釜緙虹渷鍙傛暟鐨勫嚱鏁?br />...聽def2.append(0)
...聽print par1, def1, def2, def3
...
>>> fooM.func_defaults
(1, [], 'str')

聽 鍦ㄥ嚱鏁板畾涔変腑鏈夊嚑涓己鐪佸弬鏁幫紝func_defaults涓氨浼氬寘鎷嚑涓璞★紝鏆備笖縐頒箣涓虹己鐪佸弬鏁板璞★紙濡備笂鍒椾腑鐨?錛孾]鍜?str'錛夈傝繖浜涚己鐪佸弬鏁板璞$殑鐢熷懡鍛ㄦ湡涓庡嚱鏁板璞$浉鍚岋紝浠庡嚱鏁頒嬌鐢╠ef瀹氫箟寮濮嬶紝鐩村埌鍏舵秷浜★紙濡傜敤del錛夈傛墍浠ュ嵆渚挎槸鍦ㄨ繖浜涘嚱鏁版病鏈夎璋冪敤鐨勬椂鍊欙紝浣嗗彧瑕佸畾涔変簡錛岀己鐪佸弬鏁板璞″氨浼氫竴鐩村瓨鍦ㄣ?/p>

聽 鍓嶉潰璁茶繃錛屽嚱鏁拌皟鐢ㄧ殑榪囩▼灝辨槸瀵硅薄鍦ㄥ彟澶栦竴涓悕瀛楃┖闂寸殑緇戝畾榪囩▼銆傚綋鍦ㄦ瘡嬈″嚱鏁拌皟鐢ㄦ椂錛屽鏋滄病鏈変紶閫掍換浣曞弬鏁扮粰榪欎釜緙虹渷鍙傛暟錛岄偅涔堣繖涓己鐪佸弬鏁扮殑鍚嶅瓧灝變細緇戝畾鍒板湪func_defaults涓竴涓搴旂殑緙虹渷鍙傛暟瀵硅薄涓娿?br />>>> fooM(2)
聽 鍑芥暟fooM鍐呯殑鍚嶅瓧def1灝變細緇戝畾鍒癴unc_defaults涓殑絎竴涓璞★紝def2緇戝畾鍒扮浜屼釜錛宒ef3鍒欐槸絎笁涓?br />鎵浠ユ垜浠湅鍒板湪鍑芥暟foo涓嚭鐜扮殑绱姞鐜拌薄錛屽氨鏄敱浜巔ar緇戝畾鍒扮己鐪佸弬鏁板璞′笂錛岃屼笖瀹冩槸涓涓彲鍙樺璞★紙鍒楄〃錛夛紝par.append(0)灝變細姣忔鏀瑰彉榪欎釜緙虹渷鍙傛暟瀵硅薄鐨勫唴瀹廣?/p>

聽 灝嗗嚱鏁癴oo鏀硅繘涓涓嬶紝鍙兘浼氭洿瀹規槗甯姪鐞嗚В錛?br />>>> def foo(par=[]):
...聽print id(par)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 鏌ョ湅璇ュ璞$殑鏍囪瘑鐮?br />...聽par.append(0)
...聽print par
...
>>> foo.func_defaults聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 緙虹渷鍙傛暟瀵硅薄鐨勫垵濮嬪?br />([],)
>>> id(foo.func_defaults[0])聽聽聽聽聽聽聽聽聽聽 # 鏌ョ湅絎竴涓己鐪佸弬鏁板璞$殑鏍囪瘑鐮?br />11279792聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 浣犵殑緇撴灉鍙兘浼氫笉鍚?br />>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
11279792聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 璇佹槑par緇戝畾鐨勫璞″氨鏄涓涓己鐪佸弬鏁板璞?br />[0]
>>> foo()
11279792聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 渚濇棫緇戝畾鍒扮涓涓己鐪佸弬鏁板璞?br />[0, 0]聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 璇ュ璞$殑鍊煎彂鐢熶簡鍙樺寲
>>> b=[1]
>>> id(b)
11279952
>>> foo(b)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 涓嶄嬌鐢ㄧ己鐪佸弬鏁?br />11279952聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 鍚嶅瓧par鎵緇戝畾鐨勫璞′笌澶栭儴鍚嶅瓧b鎵緇戝畾鐨勬槸鍚屼竴涓璞?br />[1, 0]
>>> foo.func_defaults
([0, 0],)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 緙虹渷鍙傛暟瀵硅薄榪樺湪閭i噷錛岃屼笖鍊煎茍娌℃湁鍙戠敓鍙樺寲
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
11279792聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 鍚嶅瓧par鍙堢粦瀹氬埌緙虹渷鍙傛暟瀵硅薄涓?br />([0, 0, 0],)

聽 涓轟簡棰勯槻姝ょ被鈥滈棶棰樷濈殑鍙戠敓錛宲ython寤鴻閲囩敤涓嬪垪鏂規硶錛?br />>>> def foo(par=[]):
...聽if par is None:
...聽聽par = []
...聽par.append(0)
...聽print par

聽 浣跨敤None浣滀負鍝ㄥ叺錛屼互鍒ゆ柇鏄惁鏈夊弬鏁頒紶鍏ワ紝濡傛灉娌℃湁錛屽氨鏂板垱寤轟竴涓柊鐨勫垪琛ㄥ璞★紝鑰屼笉鏄粦瀹氬埌緙虹渷
鍙傛暟瀵硅薄涓娿?/p>

6.鎬葷粨
聽 * python鏄竴縐嶇函綺圭殑闈㈠悜瀵硅薄璇█銆?br />聽 * 璧嬪艱鍙ユ槸鍚嶅瓧鍜屽璞$殑緇戝畾榪囩▼銆?br />聽 * 鍑芥暟鐨勪紶鍙傛槸瀵硅薄鍒頒笉鍚屽悕瀛楃┖闂寸殑緇戝畾銆?/p>

7.鍙傝冭祫鏂?br />聽 * 銆奃ive Into Python銆嬶紝Mark Pilgrim錛?a >http://diveintopython.org, 2003銆?br />聽 * 銆奝ython Objects銆嬶紝Fredrik Lundh錛?a >http://www.effbot.org/zone/python-objects.htm銆?br />聽 * 銆夾n Introduction to Python銆嬶紝David M. Beazley錛?a >http://systems.cs.uchicago.edu/~beazley/tutorial/beazley_intro_python/intropy.pdf銆?br />聽 *聽 浠嶱ython瀹樻柟緗戠珯錛?a >http://www.python.org錛変笂鍙互浜嗚В鍒版墍鏈夊叧浜嶱ython鐨勭煡璇嗐?br />

posted on 2005-06-25 10:41 I love linux 闃呰(670) 璇勮(2)緙栬緫鏀惰棌鏀惰棌鑷?65Key 鎵灞炲垎綾? Python

璇勮

re: python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ錛?2005-12-28 16:43 jarodzz

鍥犳錛屽湪python涓紝鎴戜滑搴旇鎶涘紑浼犻掑弬鏁拌繖縐嶆蹇碉紝鏃跺埢鐗㈣鍑芥暟鐨勮皟鐢ㄥ弬鏁版槸灝嗗璞$敤鍙﹀涓涓悕瀛楃┖闂寸殑鍚嶅瓧緇戝畾銆傚湪鍑芥暟涓紝涓嶈繃鏄敤浜嗗彟澶栦竴涓悕瀛楋紝浣嗚繕鏄榪欏悓涓涓璞¤繘琛屾搷浣溿?

def foolyou(a):
a=a+1

if __name__=='__main__':
b=1
foolyou(b)
print b

what is b now?聽聽鍥炲聽聽

re: python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ錛?a name="Post">2006-01-04 16:15 asdf_asdf

a=a+1 宸茬粡鏄彟涓涓?寮曠敤"浜?
>>> def foolyou(a):
print id(a)
a=a+1
print id(a)

>>> b
1
>>> foolyou(b)
148479408
148479396
>>> id(b)
148479408聽聽鍥炲聽聽



Jeff-Chen 2006-04-17 19:27 鍙戣〃璇勮
]]>
The most classical topic for Python noviceshttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/16/5651.htmlJeff-ChenJeff-ChenSun, 16 Apr 2006 08:58:00 GMThttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/16/5651.htmlhttp://m.shnenglu.com/Jeff-Chen/comments/5651.htmlhttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/16/5651.html#Feedback0http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5651.htmlhttp://m.shnenglu.com/Jeff-Chen/services/trackbacks/5651.html闃呰鍏ㄦ枃

Jeff-Chen 2006-04-16 16:58 鍙戣〃璇勮
]]>
Python 瀛︿範涔嬭礬錛堟彁楂樻葷粨錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/15/5611.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Sat, 15 Apr 2006 05:13:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/15/5611.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5611.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/15/5611.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5611.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5611.html</trackback:ping><description><![CDATA[ <strong>Note One 錛?Lists Comprehensions<br /></strong>[3*x for x in vec if x > 3]<br />[x*y for x in vec1 for y in vec2]<br />......<br />there expression can used to instead the functional programming tools such as map() ,filter(),reduce()..<br /><br /><strong>Note Two : Some functions in the modules often聽 be made used of<br />1.strip()</strong>聽:聽 Return a copy of the string s with leading and trailing聽whitespace removed.<br />>>> test_str = '聽聽 I Love Python聽 '<br />>>>聽string.strip(test_str)<br />'I Love Pyhon'聽聽聽 Note that : whitespace at the two side of the string were removed ,but it did not worked on the whitespace between string!<br /><strong>2. str()</strong> : can convert the format like int ,long , float ... into string format<br />>>> num_1 = 3.14<br />>>> num_2 = 0.618<br />>>> str(num_1) , str(num_2)<br />'3.14' '0.618'<br /><strong>3.dict()</strong><br />dict() -> new empty dictionary.<br />dict(mapping) -> new dictionary initialized from a mapping object's<br />聽聽聽 (key, value) pairs.<br />dict(seq) -> new dictionary initialized as if via:<br />聽聽聽 d = {}<br />聽聽聽 for k, v in seq:<br />聽聽聽聽聽聽聽 d[k] = v<br />dict(**kwargs) -> new dictionary initialized with the name=value pairs<br />聽聽聽 in the keyword argument list.聽 For example:聽 dict(one=1, two=2)<br /><br />e.g聽 <br />dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])<br />dict([(x, x**2) for x in (2, 4, 6)])聽聽聽聽 <br />dict(sape=4139, guido=4127, jack=4098)<br /><br /><strong>4. enumerate()</strong><br />Return an enumerate object.聽 iterable must be an other object that supports<br />iteration.聽 The enumerate object yields pairs containing a count (from<br />zero) and a value yielded by the iterable argument.聽 enumerate is useful<br />for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...<br />Code聽 show:<br />>>> for i, v in enumerate(['tic', 'tac', 'toe']):<br />...聽聽聽聽 print i, v<br />...<br />0 tic<br />1 tac<br />2 toe<br /><br /><strong>5 zip()<br /></strong>Return an enumerate object.聽 iterable must be an other object that supports<br />iteration.聽 The enumerate object yields pairs containing a count (from<br />zero) and a value yielded by the iterable argument.聽 enumerate is useful<br />for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...<br />Code Show:<br /><br /><pre>>>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print 'What is your %s? It is %s.' % (q, a) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue. </pre><br /><strong>6.sorted()<br /></strong>Code Show:<br /><pre>>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> for f in sorted(set(basket)): ... print f ... apple banana orange pear </pre><br />to be continued......<br /><br /><strong>Note Three : simple statements</strong><br /><br /><strong>7 The <tt class="keyword">yield</tt> statement</strong><br /><dl><dd class="grammar"><div id="hvzpftn" class="productions"><table><tbody><tr><td><a id="tok-yield_stmt" xml:id="tok-yield_stmt">yield_stmt</a></td><td>::=</td><td>"yield" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/exprlists.html#tok-expression_list"><font color="#002c99">expression_list</font></a></td></tr></tbody></table></div><a class="grammar-footer" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/grammar.txt" type="text/plain"><font color="#002c99">Download entire grammar as text.</font></a></dd></dl><p><a id="l2h-512" xml:id="l2h-512"></a><a id="l2h-511" xml:id="l2h-511"></a></p><p>The <tt class="keyword">yield</tt> statement is only used when defining a generator function, and is only used in the body of the generator function. Using a <tt class="keyword">yield</tt> statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. </p><p>When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator's <tt class="method">next()</tt> method repeatedly until it raises an exception. </p><p>When a <tt class="keyword">yield</tt> statement is executed, the state of the generator is frozen and the value of <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/exprlists.html#tok-expression_list"><font color="#002c99">expression_list</font></a> is returned to <tt class="method">next()</tt>'s caller. By ``frozen'' we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time <tt class="method">next()</tt> is invoked, the function can proceed exactly as if the <tt class="keyword">yield</tt> statement were just another external call. </p><p>The <tt class="keyword">yield</tt> statement is not allowed in the <tt class="keyword">try</tt> clause of a <tt class="keyword">try</tt> ... <tt class="keyword">finally</tt> construct. The difficulty is that there's no guarantee the generator will ever be resumed, hence no guarantee that the <tt class="keyword">finally</tt> block will ever get executed. </p><p></p><div id="hvzpftn" class="note"><b class="label">Note:</b> In Python 2.2, the <tt class="keyword">yield</tt> statement is only allowed when the <code>generators</code> feature has been enabled. It will always be enabled in Python 2.3. This <code>__future__</code> import statement can be used to enable the feature: <p></p><div id="hvzpftn" class="verbatim"><pre>from __future__ import generators </pre></div></div><br /><strong>8 The <tt class="keyword">raise</tt> statement</strong><a id="l2h-513" xml:id="l2h-513"></a><p></p><dl><dd class="grammar"><div id="hvzpftn" class="productions"><table><tbody><tr><td><a id="tok-raise_stmt" xml:id="tok-raise_stmt">raise_stmt</a></td><td>::=</td><td>"raise" [<a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/Booleans.html#tok-expression"><font color="#002c99">expression</font></a> ["," <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/Booleans.html#tok-expression"><font color="#002c99">expression</font></a> ["," <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/Booleans.html#tok-expression"><font color="#002c99">expression</font></a>]]]</td></tr></tbody></table></div><a class="grammar-footer" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/grammar.txt" type="text/plain"><font color="#002c99">Download entire grammar as text.</font></a></dd></dl><p>If no expressions are present, <tt class="keyword">raise</tt> re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a <tt class="exception">TypeError</tt> exception is raised indicating that this is an error (if running under IDLE, a <tt class="exception">Queue.Empty</tt> exception is raised instead). <a id="l2h-516" xml:id="l2h-516"></a><a id="l2h-514" xml:id="l2h-514"></a></p><p>Otherwise, <tt class="keyword">raise</tt> evaluates the expressions to get three objects, using <code>None</code> as the value of omitted expressions. The first two objects are used to determine the <em>type</em> and <em>value</em> of the exception. </p><p>If the first object is an instance, the type of the exception is the class of the instance, the instance itself is the value, and the second object must be <code>None</code>. </p><p>If the first object is a class, it becomes the type of the exception. The second object is used to determine the exception value: If it is an instance of the class, the instance becomes the exception value. If the second object is a tuple, it is used as the argument list for the class constructor; if it is <code>None</code>, an empty argument list is used, and any other object is treated as a single argument to the constructor. The instance so created by calling the constructor is used as the exception value. </p><p>If a third object is present and not <code>None</code>, it must be a traceback<a id="l2h-515" xml:id="l2h-515"></a> object (see section聽<a href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/types.html#traceback"><font color="#002c99">3.2</font></a>), and it is substituted instead of the current location as the place where the exception occurred. If the third object is present and not a traceback object or <code>None</code>, a <tt class="exception">TypeError</tt> exception is raised. The three-expression form of <tt class="keyword">raise</tt> is useful to re-raise an exception transparently in an except clause, but <tt class="keyword">raise</tt> with no expressions should be preferred if the exception to be re-raised was the most recently active exception in the current scope. </p><strong>Note Four : Compound Statements</strong><br /><br /><strong>9 The <tt class="keyword">try</tt> statement</strong><a id="l2h-592" xml:id="l2h-592"></a><p>The <tt class="keyword">try</tt> statement specifies exception handlers and/or cleanup code for a group of statements: </p><p></p><dl><dd class="grammar"><div id="hvzpftn" class="productions"><table><tbody><tr><td><a id="tok-try_stmt" xml:id="tok-try_stmt">try_stmt</a></td><td>::=</td><td><a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/try.html#tok-try_exc_stmt"><font color="#002c99">try_exc_stmt</font></a> | <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/try.html#tok-try_fin_stmt"><font color="#002c99">try_fin_stmt</font></a></td></tr><tr><td><a id="tok-try_exc_stmt" xml:id="tok-try_exc_stmt">try_exc_stmt</a></td><td>::=</td><td>"try" ":" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/compound.html#tok-suite"><font color="#002c99">suite</font></a></td></tr><tr><td><font color="#002c99"></font></td><td><font color="#002c99"></font></td><td><code>("except" [<a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/Booleans.html#tok-expression"><font color="#002c99">expression</font></a> ["," <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/assignment.html#tok-target"><font color="#002c99">target</font></a>]] ":" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/compound.html#tok-suite"><font color="#002c99">suite</font></a>)+</code></td></tr><tr><td></td><td></td><td><code>["else" ":" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/compound.html#tok-suite"><font color="#002c99">suite</font></a>]</code></td></tr><tr><td><a id="tok-try_fin_stmt" xml:id="tok-try_fin_stmt">try_fin_stmt</a></td><td>::=</td><td>"try" ":" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/compound.html#tok-suite"><font color="#002c99">suite</font></a> "finally" ":" <a class="grammartoken" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/compound.html#tok-suite"><font color="#002c99">suite</font></a></td></tr></tbody></table></div><a class="grammar-footer" href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/grammar.txt" type="text/plain"><font color="#002c99">Download entire grammar as text.</font></a></dd></dl><p>There are two forms of <tt class="keyword">try</tt> statement: <tt class="keyword">try</tt>...<tt class="keyword">except</tt> and <tt class="keyword">try</tt>...<tt class="keyword">finally</tt>. These forms cannot be mixed (but they can be nested in each other). </p><p>The <tt class="keyword">try</tt>...<tt class="keyword">except</tt> form specifies one or more exception handlers (the <tt class="keyword">except</tt> clauses). When no exception occurs in the <tt class="keyword">try</tt> clause, no exception handler is executed. When an exception occurs in the <tt class="keyword">try</tt> suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is ``compatible'' with the exception. An object is compatible with an exception if it is either the object that identifies the exception, or (for exceptions that are classes) it is a base class of the exception, or it is a tuple containing an item that is compatible with the exception. Note that the object identities must match, i.e. it must be the same object, not just an object with the same value. <a id="l2h-593" xml:id="l2h-593"></a></p><p>If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. </p><p>If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire <tt class="keyword">try</tt> statement raised the exception). </p><p>When a matching except clause is found, the exception's parameter is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) </p><p>Before an except clause's suite is executed, details about the exception are assigned to three variables in the <tt class="module">sys</tt><a id="l2h-605" xml:id="l2h-605"></a> module: <code>sys.exc_type</code> receives the object identifying the exception; <code>sys.exc_value</code> receives the exception's parameter; <code>sys.exc_traceback</code> receives a traceback object<a id="l2h-594" xml:id="l2h-594"></a> (see section聽<a href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/types.html#traceback"><font color="#002c99">3.2</font></a>) identifying the point in the program where the exception occurred. These details are also available through the <tt class="function">sys.exc_info()</tt> function, which returns a tuple <code>(<var>exc_type</var>, <var>exc_value</var>, <var>exc_traceback</var>)</code>. Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception. <a id="l2h-596" xml:id="l2h-596"></a></p><p>The optional <tt class="keyword">else</tt> clause is executed if and when control flows off the end of the <tt class="keyword">try</tt> clause.<a href="mk:@MSITStore:D:\Document\E-Book\Python24.chm::/ref/try.html#foot6714" name="tex2html10"><sup><font color="#002c99">7.1</font></sup></a> Exceptions in the <tt class="keyword">else</tt> clause are not handled by the preceding <tt class="keyword">except</tt> clauses. <a id="l2h-597" xml:id="l2h-597"></a><a id="l2h-598" xml:id="l2h-598"></a><a id="l2h-599" xml:id="l2h-599"></a><a id="l2h-600" xml:id="l2h-600"></a></p><p>The <tt class="keyword">try</tt>...<tt class="keyword">finally</tt> form specifies a `cleanup' handler. The <tt class="keyword">try</tt> clause is executed. When no exception occurs, the <tt class="keyword">finally</tt> clause is executed. When an exception occurs in the <tt class="keyword">try</tt> clause, the exception is temporarily saved, the <tt class="keyword">finally</tt> clause is executed, and then the saved exception is re-raised. If the <tt class="keyword">finally</tt> clause raises another exception or executes a <tt class="keyword">return</tt> or <tt class="keyword">break</tt> statement, the saved exception is lost. A <tt class="keyword">continue</tt> statement is illegal in the <tt class="keyword">finally</tt> clause. (The reason is a problem with the current implementation - this restriction may be lifted in the future). The exception information is not available to the program during execution of the <tt class="keyword">finally</tt> clause. <a id="l2h-601" xml:id="l2h-601"></a></p><p>When a <tt class="keyword">return</tt>, <tt class="keyword">break</tt> or <tt class="keyword">continue</tt> statement is executed in the <tt class="keyword">try</tt> suite of a <tt class="keyword">try</tt>...<tt class="keyword">finally</tt> statement, the <tt class="keyword">finally</tt> clause is also executed `on the way out.' A <tt class="keyword">continue</tt> statement is illegal in the <tt class="keyword">finally</tt> clause. (The reason is a problem with the current implementation -- this restriction may be lifted in the future). <br /></p><img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5611.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-15 13:13 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/15/5611.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Serious Recommend:Python Documentation Tips and Trickshttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5535.htmlJeff-ChenJeff-ChenFri, 14 Apr 2006 05:27:00 GMThttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5535.htmlhttp://m.shnenglu.com/Jeff-Chen/comments/5535.htmlhttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5535.html#Feedback0http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5535.htmlhttp://m.shnenglu.com/Jeff-Chen/services/trackbacks/5535.html

Jeff-Chen 2006-04-14 13:27 鍙戣〃璇勮
]]>
鏃犳硶鍦╡clipse涓洿鏂皃ydev錛堜緵閬囧埌姝ょ被闂鐨勬湅鍙嬪弬鑰冿級http://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5525.htmlJeff-ChenJeff-ChenFri, 14 Apr 2006 04:00:00 GMThttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5525.htmlhttp://m.shnenglu.com/Jeff-Chen/comments/5525.htmlhttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5525.html#Feedback0http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5525.htmlhttp://m.shnenglu.com/Jeff-Chen/services/trackbacks/5525.html 澶寸柤浜嗗緢涔?eclipse涓棤娉曟甯告洿鏂皃ydev銆傛垜鍙堝洖澶存鏌ワ紝鍩熷悕鏄紜殑錛屼絾鏄繕鏄嚭鐜伴敊璇?br />鎯充簡寰堜箙錛屽湪緗戜笂鍒板鎵懼叧浜庢綾婚棶棰樼殑鏂囩珷錛屼絾鏄敹鑾蜂笉澶э紝鍙互璇村嚑涔庢壘涓嶅埌涔嬬被鏂囩珷銆?br />騫歌繍鐨勪簨 錛屾垜姝e湪瀵繪壘鍏朵粬鎻掍歡鏃跺欏拰瀵熺湅鍏朵粬python寮鍙戝伐鍏風殑鏃跺欙紝鏃犳剰涓偣鍑轟簡IBM 鐨勪笅闈㈣繖涓〉闈€?br />欏挎椂鎴戝氨鎭嶇劧澶ф偀錛屾墠鍙戠幇鏃犳硶鏇存柊鐨勫師鍥狅細
鍥犱負鍏徃鐨勫皝鎺変簡鍑犱釜绔彛錛岄槻姝嬌鐢ㄩ潪宸ヤ綔鑼冨洿鍐呯殑濞變箰杞歡鎴栧叾浠栫殑鑱婂ぉ杞歡錛?br />鑰宔clipse鐨勬洿鏂版ā寮忓熀浜庡熀浜嶧TP妯″紡鐨勩?br />褰撶劧榪欐槸閫犳垚鏃犳硶鏇存柊鐨勫師鍥犱箣涓錛屽彲鑳借繕鏈夊叾浠栧師鍥犱絾鏄垜娌℃兂鍒幫紝涔熸病閬囪榪囥?br />
quote:

涓嬭澆鏂規硶錛欶TP銆丠TTP 鍜屼笅杞藉姪鎵?/span>

IBM developerWorks 鎻愪緵浜嗕笁縐嶄笅杞芥柟娉曪細FTP銆丠TTP 浠ュ強鐢ㄤ簬澶ф枃浠朵笅杞界殑涓嬭澆鍔╂墜錛?Download Director錛夈傚ぇ澶氭暟鎯呭喌涓嬶紝FTP 瑕佹瘮 HTTP 鏇村彲闈狅紝騫跺彲緙╃煭涓嬭澆鏃墮棿銆傛渶鏂扮殑嫻忚鍣ㄥ湪榪涜 FTP 涓嬭澆鏃朵細鏄劇ず鏃墮棿浼拌鍜屼紶杈撻熷害銆傚湪鏈変簺鎯呭喌涓嬶紝嫻忚鍣ㄨ繕鍙互緇紶涓柇鐨?FTP 涓嬭澆銆傝鍒╃敤 FTP 鐨勮繖縐嶆柟渚挎э紝鍦ㄤ嬌鐢?HTTP 涓嬭澆涔嬪墠棣栧厛灝濊瘯涓涓?FTP 涓嬭澆鏂瑰紡銆?/p>

濡傛灉鎮ㄤ嬌鐢?FTP 絀胯繃闃茬伀澧欎笅杞芥椂閬囧埌浜嗛棶棰橈紝鍐嶈瘯鐢?HTTP 涓嬭澆銆傝櫧鐒朵嬌鐢?HTTP 鏂瑰紡榪涜澶ф枃浠朵笅杞芥椂涓嶅 FTP 鍙潬錛屼絾 Internet Explorer 5.01 鍙婁互涓婄増鏈湁鏃惰兘澶熸仮澶嶄腑鏂殑 HTTP 涓嬭澆銆侶TTP 涓嬭澆鑳藉絀胯繃闃茬伀澧欙紝鍥犱負瀹冧嬌鐢ㄤ簡绔彛鍙?80 鏉ヨ繘琛屾暟鎹紶杈撱備互涓嬩笅杞借涓哄拰閿欒鍙兘鏄亣鍒頒簡闃茬伀澧欓棶棰橈細

  • 涓磋繎緇撴潫浣嗗張娌℃湁瀹屾垚鏃朵笅杞界獊鐒跺仠姝€?
  • 姣忔鍦ㄥ悓涓鍦版柟鍋滄涓嬭澆銆?
  • 閿欒 400 琛ㄧず浠g悊鍔犺澆澶辮觸銆?
  • 鍑虹幇涓鏉℃秷鎭〃鏄庢枃浠剁被鍨嬫湭鎵懼埌銆?
  • 鍑虹幇涓鏉℃秷鎭〃鏄庨〉闈㈠凡緇忕Щ璧般?


Jeff-Chen 2006-04-14 12:00 鍙戣〃璇勮
]]>
鐢?Eclipse 鍜?Ant 榪涜 Python 寮鍙?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5521.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Fri, 14 Apr 2006 03:34:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5521.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5521.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5521.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5521.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5521.html</trackback:ping><description><![CDATA[ <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr valign="top"> <td width="100%"> <h1>鐢?Eclipse 鍜?Ant 榪涜 Python 寮鍙?/h1> <p id="subtitle">鐢?Eclipse IDE 鍜?Apache Ant 鏋勫緩宸ュ叿榪涜 Python 寮鍙?/p> <img class="display-img" height="6" alt="" src="http://www.ibm.com/i/c.gif" width="1" /> </td> <td class="no-print" width="192"> <img height="18" alt="developerWorks" src="http://www-128.ibm.com/developerworks/cn/i/dw.gif" width="192" /> </td> </tr> </tbody> </table> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr valign="top"> <td width="10"> <img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="10" /> </td> <td width="100%"> <table class="no-print" cellspacing="0" cellpadding="0" width="160" align="right" border="0"> <tbody> <tr> <td width="10"> <img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="10" /> </td> <td> <table cellspacing="0" cellpadding="0" width="150" border="0"> <tbody> <tr> <td class="v14-header-1-small">鏂囨。閫夐」</td> </tr> </tbody> </table> <table class="v14-gray-table-border" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td class="no-padding" width="150"> <table cellspacing="0" cellpadding="0" width="143" border="0"> <img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" /> <form name="email" action="https://www-128.ibm.com/developerworks/secure/email-it.jsp"> <input type="hidden" value="Python 鏄竴縐嶉潪甯哥伒媧誨己澶х殑鍔ㄦ佽剼鏈紪紼嬭璦錛屽叿鏈夊畬鏁寸殑闈㈠悜瀵硅薄鐗規с傛湰鏂囩潃閲嶄粙緇嶄簡濡備綍浣跨敤鍩轟簬 Java 鎶鏈殑嫻佽寮鍙戝伐鍏?Eclipse 鍜?Ant 榪涜 Python 寮鍙戙? name="body" /> <input type="hidden" value="鐢?Eclipse 鍜?Ant 榪涜 Python 寮鍙? name="subject" /> <input type="hidden" value="cn" name="lang" /> <script language="JavaScript" type="text/javascript"> <!-- document.write('<tr valign="top"><td width="8"><img src="http://www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="http://www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="灝嗘欏典綔涓虹數瀛愰偖浠跺彂閫? /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>灝嗘欏典綔涓虹數瀛愰偖浠跺彂閫?/b></a></p></td></tr>'); //--> </script> <tbody> <tr valign="top"> <td width="8"> <img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" /> </td> <td width="16"> <img height="16" alt="灝嗘欏典綔涓虹數瀛愰偖浠跺彂閫? src="http://www.ibm.com/i/v14/icons/em.gif" width="16" vspace="3" /> </td> <td width="122"> <p> <a class="smallplainlink" href="javascript:document.email.submit();"> <b> <font color="#5c81a7" size="2">灝嗘欏典綔涓虹數瀛愰偖浠跺彂閫?/font> </b> </a> </p> </td> </tr> <noscript> <tr valign="top"> <td width="8"> <img alt="" height="1" width="8" src="http://www.ibm.com/i/c.gif" /> </td> <td width="16"> <img alt="" width="16" height="16" src="http://www.ibm.com/i/c.gif" /> </td> <td class="small" width="122"> <p> <span id="hvzpftn" class="ast">鏈樉紺洪渶瑕?JavaScript 鐨勬枃妗i夐」</span> </p> </td> </tr> </noscript> </tbody> </form> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <!--START RESERVED FOR FUTURE USE INCLUDE FILES--> <!-- 03/20/06 updated by gretchen --> <br /> <table cellspacing="0" cellpadding="0" width="150" border="0"> <tbody> <tr> <td class="v14-header-2-small">鏈鏂版帹鑽?/td> </tr> </tbody> </table> <table class="v14-gray-table-border" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td class="no-padding" width="150"> <table cellspacing="0" cellpadding="0" width="143" border="0"> <tbody> <tr valign="top"> <td width="8"> <img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" /> </td> <td> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/fw_bold.gif" width="16" vspace="3" border="0" /> </td> <td width="125"> <p> <a class="smallplainlink" > <font color="#5c81a7" size="2">Java 搴旂敤寮鍙戞簮鍔ㄥ姏 錛?涓嬭澆鍏嶈垂杞歡錛屽揩閫熷惎鍔ㄥ紑鍙?/font> </a> </p> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <!--END RESERVED FOR FUTURE USE INCLUDE FILES--> <br /> </td> </tr> </tbody> </table> <p>綰у埆: 鍒濈駭</p> <p> <a > <font color="#996699">Ron Smith</font> </a>, 璐熻矗浜?br /></p> <p>2004 騫?6 鏈?01 鏃?/p> <blockquote>Python 鏄竴縐嶉潪甯哥伒媧誨己澶х殑鍔ㄦ佽剼鏈紪紼嬭璦錛屽叿鏈夊畬鏁寸殑闈㈠悜瀵硅薄鐗規с備紬澶氱殑鏀寔鑰呮寚鍑猴紝Python 璇█涓庡叾浠栬璦鐩告瘮鑳芥洿蹇洿鏈夋晥鍦拌〃杈懼嚭浠栦滑鐨勬剰鍥俱備絾鏄粠 Java 鎶鏈劉 鎴?Microsoft廬 .NET 鍒氬垰杞埌 Python 鐨勪漢浼氬彂鐜幫紝鍔熻兘涓板瘜鑰岀簿鑷寸殑 IDE 鍜屽紑鍙戝伐鍏烽兘涓嶈浜嗐傞偅浜涘紑鍙戜漢鍛樺彲浠ヤ粠浠栦滑鐔熸倝鐨?Java 寮鍙戝伐鍏蜂腑鎵懼埌瑙e喅鏂規銆傛湰鏂囩潃閲嶄粙緇嶄簡濡備綍浣跨敤鍩轟簬 Java 鎶鏈殑嫻佽寮鍙戝伐鍏?Eclipse 鍜?Ant 榪涜 Python 寮鍙戙?/blockquote> <!--START RESERVED FOR FUTURE USE INCLUDE FILES--> <!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --> <!--END RESERVED FOR FUTURE USE INCLUDE FILES--> <p> <a name="N1003D"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">綆浠?/font> </span> </a> </p> <p>澶氬勾浠ユ潵錛?Java 璇█鍜?Python 闃佃惀涔嬮棿涓鐩村瓨鍦ㄥぇ閲忕殑寮傝姳鎺堢矇鐜拌薄銆傚湪榪欐柟闈綔鍑虹獊鍑鴻〃鐜囩殑鍙兘鏄?Jython銆傝繖鏄竴涓函綺圭敤 Java 瀹炵幇鐨?Python 榪愯鏃剁幆澧冦傛寜鐓ц繖涓璇存硶錛屾偍灝嗙爺絀跺浣曠敤 Eclipse IDE 鍜?Ant 鏋勫緩涓庨儴緗插伐鍏峰疄鐜?Python 寮鍙戙侲clipse 鍜?Ant 鏄潪甯告祦琛岀殑宸ュ叿錛屽畠浠壒鎬т赴瀵屻佸彲鎵╁睍鎬у己銆佽屼笖寮鏀炬簮浠g爜錛汸ython 涔熷叿鏈夌浉鍚岀殑鍝佽川銆侾yDev 鍜?PyAntTasks 鍒嗗埆鏄?Eclipse 鍜?Ant 鐨勬墿灞曪紝鏈変簡瀹冧滑灝卞彲鑳界敤榪欎簺 Java 宸ュ叿寮鍙?Python銆傛湰鏂囦粠涓嬭澆瀹夎鎵闇鐨勫伐鍏蜂笌鎵╁睍寮濮嬭璧楓備負浜嗚В閲婂浣曞湪 Python 寮鍙戜腑浣跨敤 Eclipse 鍜?Ant錛屾垜灝嗙敤瀹為檯鐨?Python 浠g爜渚嬪瓙璇誨彇 RSS 璧勬簮銆?</p> <p>鏈枃涓嶄細娑夊強 Eclipse銆丄nt銆丳ython 鐨勭粏鑺傘傛湁鍏寵繖浜涜瘽棰樼殑娣卞叆璁ㄨ錛岃鍙傞槄 <a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺備腑鐨勯摼鎺ャ?</p> <p> <a name="N1004D"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Python 鏀寔鎯呭喌</font> </strong> </span> </a> </p> <p>鏈枃鐢ㄥ埌鐨勮蔣浠墮兘鍦?CPython 2.3 涓嬫祴璇曡繃銆傞櫎浜嗗嚑涓紓甯告儏鍐典箣澶栵紝搴旇涔熻兘鍦?Jython 涓繍琛屻傜壒鍒渶瑕佹寚鍑猴紝PyDev 璋冭瘯鍣ㄧ洰鍓嶄笉鏀寔 Jython銆傚彟涓涓尯鍒槸閫氳繃 Jython 鎵ц鐨勮剼鏈湪浠?PyDev 涓繍琛屼箣鍚庡氨杞叆浜や簰妯″紡錛岃繖鏍峰氨蹇呴』鎵嬪姩鏉姝匯侾yDev 緙栬緫鍣ㄤ笌 Jython 鐨勬簮浠g爜鍏煎錛孭ython Ant 浠誨姟闄?py-doc 浠誨姟涔嬪涔熷拰 Jython 鍏煎銆?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="N10056"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">浣跨敤 Eclipse 榪涜 Python 寮鍙?/font> </span> </a> </p> <p> <a name="N1005C"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Eclipse 姒傝堪</font> </strong> </span> </a> </p> <p>Eclipse 鏄竴涓?Java 鎶鏈泦鎴愬紑鍙戠幆澧冿紝鐢?IBM 寮鍙戯紝騫跺紑鏀懼叾婧愪唬鐮併傚畠鏄?IBM 鍟嗕笟杞歡 WebSphere Application Development 鐜浠ュ強鍏朵粬澶氱宸ュ叿鐨勫熀紜銆侲clipse 鐨勫紑鍙戠ぞ鍖洪潪甯告椿璺冿紝浠栦滑涓嶄粎寮鍙?Eclipse 鏈韓錛岃繕寮鍙戝ぇ閲忕殑鎻掍歡渚?Eclipse 浣跨敤銆傛湁鍏?Eclispe 鍜?Eclipse 鎻掍歡鐨?Web 绔欑偣錛岃鍙傞槄 <a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺備腑鐨勯摼鎺ャ傚敖綆′粠浼犵粺涓婅 Eclipse 鏄竴縐?Java 寮鍙戝伐鍏鳳紝浣嗘槸涓浜涙彃浠剁殑瀛樺湪浣垮緱鍦?Eclipse 涓紑鍙戝叾浠栬璦鐨勭▼搴忔垚涓哄彲鑳斤紝濡?C/C++銆丳ython 鍜?Perl銆?</p> <p>鍦?Eclipse 涓紝婧愪唬鐮佽緇勭粐鍒伴」鐩紙project錛変腑銆傞」鐩彲浠ュ姞杞姐佸嵏杞藉拰瀵煎叆銆侲clipse 鐢ㄦ埛鐣岄潰鐨勭粨鏋勫垝鍒嗕負瑙嗗浘錛圴iew錛変笌緙栬緫鍣紙Editor錛夈傝鍥句笌緙栬緫鍣ㄧ殑渚嬪瓙鍖呮嫭錛氭簮浠g爜澶х翰瑙嗗浘銆丣ava 婧愪唬鐮佺紪杈戝櫒銆丳ython 婧愪唬鐮佺紪杈戝櫒鍜屾枃浠剁郴緇熷鑸鍥俱侲clipse 鐢ㄦ埛鐣岄潰涓渶鍏抽敭鐨勯殣鍚蹇靛氨鏄?<i>瑙嗚錛坧erspective錛?/i>銆傝瑙掓槸閫氬父鍦ㄦ墽琛屾煇縐嶇被鍨嬫椿鍔ㄦ椂涓璧蜂嬌鐢ㄧ殑涓緇勮鍥俱侲clipse 涓殑鏍囧噯瑙嗚鍖呮嫭錛欴ebug銆丣ava Browsing銆丣ava銆丣ava Type Hierarchy銆丳lug-in Development銆丆VS Repository Exploring銆丷esource 鍜?Install/Update銆傜洰鍓嶈繕涓嶅瓨鍦ㄥ崟鐙殑 Python 瑙嗚銆傚湪榪涜 Python 寮鍙戞椂錛屾垜閫氬父浣跨敤 Resource 瑙嗚鍜?Debug 瑙嗚銆?</p> <p> <a name="N1006F"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">瀹夎 PyDev</font> </strong> </span> </a> </p> <p>棣栧厛錛屼粠 Eclipse Web 绔欑偣涓婁笅杞?Eclipse錛堣鍙傞槄 <a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺備腑鐨勯摼鎺ワ級錛屽茍鏍規嵁鎮ㄧ殑騫沖彴錛屾寜鐓т笅闈㈢殑瀹夎鎸囧崡瀹夎 Eclipse錛?</p> <p>Eclipse 鐨勬洿鏂版満鍒朵嬌 PyDev 鎻掍歡鐨勫畨瑁呮洿鍔犲鏄撱備粠 Eclipse 涓夋嫨 <b>Help > Software Updates > Update Manager</b>錛屽惎鍔?Install/Update 瑙嗚銆傚湪宸︿笅瑙掔殑 Feature Updates 瑙嗗浘涓紝灝?PyDev 鎻掍歡鏇存柊绔欑偣浣滀負鏂扮殑 Site Bookmark 娣誨姞鍒扳淪ites to Visit鈥濇枃浠跺す涓嬨侲clipse 鐨?PyDev 鏇存柊绔欑偣 URL 涓?http://pydev.sf.net/updates/銆傜幇鍦紝Feature Updates 緙栬緫鍣ㄤ腑搴旇鏄劇ず鍑衡淧yDev鈥濊繖涓鐗規с傚湪 Feature Updates 緙栬緫鍣ㄤ腑錛屽睍寮 <b>PyDev > Other</b>錛岄夋嫨鍏朵腑鏄劇ず鐨?PyDev 鐗規э紙鑷沖皯搴旇鏄?0.4.1錛夈傜劧鍚庨夋嫨 鈥淚nstall Now鈥濆畨瑁呰鐗規с侲clipse 灝嗕笅杞?PyDev 鎻掍歡錛屽茍灝嗗叾瀹夎鍒?Eclipse 涓?</p> <p> <a name="N10085"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">瀵煎叆鏍蜂緥欏圭洰</font> </strong> </span> </a> </p> <p>涓鴻闂湰欏圭洰涓嬌鐢ㄧ殑鏍蜂緥浠g爜錛屽彲鍏堜笅杞?zip 鏂囦歡錛堣鍙傞槄 <a ><font color="#996699">鍙傝冭祫鏂?/font></a>涓鑺傦級錛屽湪鏂囦歡緋葷粺涓睍寮璇?zip 鏂囦歡錛岀劧鍚庡皢鍏朵腑鐨勯」鐩鍏?Eclipse銆傚鍏ラ」鐩殑鏂規硶鏄厛鍒囨崲鍒?Resource 瑙嗚錛岄夋嫨 <b>File > Import</b>錛屽啀閫夋嫨鈥淓xisting Project into Workspace鈥濓紝鐒跺悗閫夋嫨鎮ㄥ睍寮 zip 鏂囦歡鐨勪綅緗傝繖鏃訛紝Navigator 瑙嗗浘涓簲璇ュ嚭鐜?feedParserTest 欏圭洰銆?</p> <p>鏍蜂緥欏圭洰涓凡緇忓寘鍚簡 Fead Parser 閫氱敤璧勬簮瑙f瀽搴擄紝璇ュ簱鎸?Python 寮鏀炬簮浠g爜璁稿彲鍗忚鍙戝竷銆傛湁鍏?Feed Parser 欏圭洰 Web 緗戠珯鐨勯摼鎺ワ紝璇峰弬闃?<a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺傘?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="N1009C"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">PyDev 婕父</font> </span> </a> </p> <p>鐜板湪寮濮嬪涔犲浣曢氳繃宸插鍏ョ殑欏圭洰浜嗚В PyDev 鐨勭壒鎬с侾yDev 姝e浜庡紑鍙戣繃紼嬩腑錛屼絾宸茬粡鏄潪甯擱珮鏁堢殑 Python 寮鍙戠幆澧冦傜幇鍦ㄧ殑 PyDev 涓昏鍖呮嫭浠ヤ笅鐗規э細</p> <ul> <li>鍖呭惈 Python 璇硶楂樹寒鏄劇ず鐗規х殑 Python 緙栬緫鍣ㄣ? </li> <li>榪涜 Python 璇硶鍒嗘瀽錛屽茍鍦?Python 緙栬緫鍣ㄥ拰 Tasks 瑙嗗浘涓珮浜樉紺洪敊璇? </li> <li>鍙皢鍒惰〃絎﹁漿鎹㈡垚絀烘牸鐨勯夐」銆? </li> <li>Outline 瑙嗗浘鏄劇ず瀵煎叆鐨勫簱銆佺被浠ュ強鍑芥暟銆? </li> <li>緇堢瑙嗗浘涓殑 Python 鍫嗘爤璺熻釜淇℃伅鍙秴閾炬帴鍒版簮浠g爜涓? </li> <li>婧愪唬鐮佸唴閮ㄧ殑瓚呴摼鎺ワ紱鍚屼竴妯″潡鍐呯殑瀵煎叆鍜屽嚱鏁拌皟鐢ㄥ彲閫氳繃瓚呴摼鎺ヨ繘琛屽鑸? </li> <li>浠?Navigator 瑙嗗浘涓繍琛?Python 鑴氭湰鐨勮兘鍔涖? </li> <li>璋冭瘯鍣ㄦ敮鎸佹柇鐐廣佷唬鐮佸崟姝ユ墽琛屼互鍙婃樉紺哄彉閲忕殑鍊箋?</li> </ul> <p> <a name="N100C0"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">PyDev 閫夐」紿楀彛</font> </strong> </span> </a> </p> <p>閫氳繃 <b>Window > Preferences</b>錛屽茍閫夋嫨 PyDev錛堣鍙傞槄鍥?1錛夛紝渚垮彲璁塊棶 PyDev 閫夐」銆傜涓緇勯夐」鍙互鏀瑰彉 PyDev 鍦ㄦ簮浠g爜涓鐞嗗埗琛ㄧ鐨勬柟寮忥紝榪樺彲浠ユ敼鍙樿娉曞厓绱犵殑棰滆壊銆?</p> <br /> <a name="N100CE"> <b>鍥?1. PyDev 閫夐」紿楀彛</b> </a> <br /> <img alt="PyDev 閫夐」紿楀彛" src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/pyDevPrefs.jpg" /> <br /> <p> <a name="N100DB"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">璁劇疆 Python 瑙i噴鍣?/font> </strong> </span> </a> </p> <p>PyDev Debug 閫夐」鍙互閫夋嫨 Python 瑙i噴鍣紝渚涙墽琛?Python 浠g爜鏃朵嬌鐢ㄣ傚鏋?PyDev 鏃犳硶鎵懼埌 Python 瑙i噴鍣紝鎴栬呮兂浣跨敤鍒殑瑙i噴鍣紝鍙湪姝よ緗紙璇峰弬闃呭浘 2錛夈?</p> <br /> <a name="N100E6"> <b>鍥?2. PyDev Debug 閫夐」</b> </a> <br /> <img alt="PyDev Debug 閫夐」" src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/pyDevDebugPrefs.jpg" /> <br /> <p> <a name="N100F3"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">澶勭悊婧愪唬鐮?/font> </strong> </span> </a> </p> <p>鎴戠殑澶ч儴鍒?Python 宸ヤ綔閮芥槸鍦?Resource 瑙嗚涓畬鎴愮殑銆備嬌鐢ㄦ柟娉曟槸鍏堝垏鎹㈠埌 Resource 瑙嗚錛岀劧鍚庡湪宸︿笂瑙掔殑 Navigator 瑙嗗浘涓弻鍑?feedParserTest/src/feedparserTest/FeedparserTest.py 鏂囦歡銆侾ython 緙栬緫鍣ㄦ墦寮璇ユ枃浠訛紝瀵?Python 璇硶榪涜瑙f瀽錛屽畬鎴愯緗鑹插拰璇硶媯鏌ョ殑宸ヤ綔錛堣鍙傞槄鍥?3錛夈?</p> <br /> <a name="N100FE"> <b>鍥?3. Python 緙栬緫鍣?/b> </a> <br /> <img alt="Python 緙栬緫鍣? src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/pythonEditor.jpg" /> <br /> <p>濡傛灉婧愪唬鐮佷腑鏈変換浣曢敊璇紝鍒欐樉紺哄湪鍙充笅瑙掔殑 Tasks 瑙嗗浘涓樉紺哄嚭鏉ャ傚弻鍑?Tasks 瑙嗗浘涓殑閿欒錛屼究鍙壘鍒伴偅鏉¤鍘岀殑浠g爜琛屻?</p> <p>Outline 瑙嗗浘鍦ㄥ乏涓嬭錛屽叾涓敤涓縐嶄究浜庢祻瑙堢殑緇撴瀯鏄劇ず鍑哄綋鍓嶆鍦ㄧ紪杈戠殑鏂囦歡銆傚鍏ョ殑搴撱佺被銆佸嚱鏁板叏閮芥樉紺哄嚭鏉ワ紝閫氳繃鍙屽嚮 Outline 瑙嗗浘涓殑欏圭洰錛屼究鍙互瀹炵幇瀵艱埅銆侾yDev 鍦ㄧ紪杈?Python 鏂囦歡鐨勮繃紼嬩腑瀵歸綈榪涜棰勫厛瑙f瀽鐨勫伐浣滐紝鍚屾椂鏇存柊 Outline 瑙嗗浘錛屾墽琛岃娉曟鏌ワ紝騫剁敤涓嶅悓棰滆壊鏄劇ず璇硶鍏冪礌銆?</p> <p> <a name="N10111"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">緙栬緫鍣ㄧ殑鐗規?/font> </strong> </span> </a> </p> <p>PyDev 0.4 鐗堝湪 Python 婧愪唬鐮佺紪杈戝櫒涓負鍑芥暟鍜屽鍏ュ簱鍔犲叆浜嗚秴閾炬帴鐨勭壒鎬с傚鏋滃湪瓚婅繃鏌愰」瀵煎叆鎴栧嚱鏁拌皟鐢紙蹇呴』鍦?<code>PYTHONPATH</code> 鐩綍涓級鐨勫悓鏃舵寜涓?Control 閿紝PyDev 灝辮兘鏄劇ず鍑轟竴涓秴閾炬帴錛岃繖鏍鋒偍鍙互鍦ㄥ鍏ュ簱鎴栧嚱鏁扮殑婧愪唬鐮佷箣闂村鑸傝娉ㄦ剰錛屼負浜嗗湪鎮ㄨ嚜宸辯殑婧愪唬鐮佷腑璺ㄦā鍧椾嬌鐢ㄨ鐗規э紙浠庝竴涓ā鍧楅摼鎺ュ埌鍙︿竴涓ā鍧楋級錛屽繀欏諱慨鏀?<code>PYTHONPATH</code> 鐜鍙橀噺錛屽湪鍏朵腑鍔犲叆榪欎簺妯″潡錛岃繖鏍?PyDev 灝卞彲浠ユ壘鍒板畠浠簡銆?</p> <p>浜轟滑宸茬粡寮濮嬪皢涓浜涗紭寮傜殑婧愪唬鐮佺紪杈戠壒鎬у姞鍏ユ渶鏂扮増鏈殑 PyDev 涓紝鍏朵腑灝卞寘鎷唬鐮佸潡娉ㄩ噴涓庡彇娑堟敞閲婏紝浠ュ強浠g爜宸﹀彸縐諱綅錛堣鍙傞槄鍥?4錛夈?</p> <br /> <a name="N10127"> <b>鍥?4. PyDev 緙栬緫鍣ㄧ殑鍏朵粬鐗規?/b> </a> <br /> <img alt="PyDev 緙栬緫鍣ㄧ殑鍏朵粬鐗規? src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/editorFeatures.jpg" /> <br /> <p> <a name="N10134"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">榪愯 Python 鑴氭湰</font> </strong> </span> </a> </p> <p>濡傛灉涓嶈兘鎵ц浠g爜錛岄偅涔?IDE 涔熶笉鏄お鏈夌敤銆備負鎵ц Python 浠g爜錛屽彲浠?Navigator 瑙嗗浘涓夋嫨 feedparser.py 鏂囦歡錛岀敤鍙抽敭鐐瑰嚮錛岀劧鍚庨夋嫨 <b>Python > Run</b>銆傞殢鍚庝細鏄劇ず Python 鐨勫惎鍔ㄩ厤緗獥鍙o紙璇峰弬闃呭浘 5錛夈?</p> <br /> <a name="N10142"> <b>鍥?5. Python 鍚姩閰嶇疆</b> </a> <br /> <img alt="Python 鍚姩閰嶇疆" src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/pythonLaunch.jpg" /> <br /> <p>Python 鍚姩閰嶇疆紿楀彛涓彲浠ュ畾涔夎剼鏈墽琛岀殑褰撳墠鐩綍錛屼紶閫掔粰鑴氭湰鐨勫弬鏁幫紝浠ュ強鐢ㄥ摢涓涓?Python 瑙i噴鍣ㄨ繍琛岃剼鏈俧eedparser.py 浠ヤ竴涓?RSS URL 浣滀負鍙傛暟錛屾墍浠ュ彲鍦ㄥ弬鏁板瓧孌典腑濉叆 URL錛屽 http://www.ibm.com/developerworks/news/dw_dwtp.rss銆傚叾浣欑殑緙虹渷瀹氫箟灝卞彲浠ヤ簡錛屾墍浠ュ崟鍑?<b>Run</b>銆?</p> <p>鑴氭湰鎵ц鏃惰緭鍑轟俊鎭樉紺哄湪 Console 紿楀彛涓傚鏋滄湁閿欒鍑虹幇錛孋onsole 紿楀彛涓皢鏄劇ず鍫嗘爤璺熻釜淇℃伅錛屽叾涓殑姣忎竴琛岄兘鍙互閫氳繃瓚呴摼鎺ユ壘鍒?Python 婧愪唬鐮併?</p> <p> <a name="N10158"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Python 璋冭瘯鍣?/font> </strong> </span> </a> </p> <p>Python 璋冭瘯鍣ㄦ槸鏈榪戞墠鍔犲叆 PyDev 鎻掍歡涓殑銆傝浣跨敤璋冭瘯鍣紝鍙湪 Python 緙栬緫鍣ㄤ腑鎯充腑鏂殑浠g爜琛岀殑宸︿晶鐐瑰嚮錛岃緗柇鐐廣傚湪鍥?6 涓紝鎴戝湪 feedparser.py 鐨?1830 琛屽璁劇疆浜嗘柇鐐廣傜劧鍚庡湪 Navigator 瑙嗗浘涓夋嫨榪欎釜 Python 妯″潡錛岀偣鍑誨彸閿紝閫夋嫨鈥淧ython > Debug...鈥濄傝繖鏃跺皢鏄劇ず涓庡墠闈㈢浉浼肩殑涓涓惎鍔ㄩ厤緗獥鍙c傜偣鍑?<b>Debug</b> 榪涘叆 Debug 瑙嗚錛屽悓鏃跺惎鍔ㄨ皟璇曞櫒銆?</p> <br /> <a name="N10166"> <b>鍥?6. Python 璋冭瘯鍣?/b> </a> <br /> <img alt="Python 璋冭瘯鍣? src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/debugger.jpg" /> <br /> <p>宸︿笂瑙掔殑 Debug 瑙嗗浘鏄劇ず褰撳墠姝e湪鎵ц鐨勮繘紼嬪拰綰跨▼錛屽彸涓婅鐨?Variables 瑙嗗浘鏄劇ず褰撳墠榪愯鍩熶腑鐨勬墍鏈夊彉閲忥紝Python 緙栬緫鍣ㄤ細鏄劇ず璋冭瘯鍣ㄧ洰鍓嶅仠鍦ㄥ摢鏉¤鍙ヤ笂錛屽悓鏃舵墍鏈夌殑杈撳嚭淇℃伅閮芥樉紺轟笌 Console 瑙嗗浘涓傝皟璇曞櫒鍙互閫氳繃 Debug 瑙嗗浘搴曢儴鐨勬寜閽垨 Run 鑿滃崟榪涜鎺у埗銆?</p> <p> <a name="N10176"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">鍏朵粬 Eclipse 鐗規?/font> </strong> </span> </a> </p> <p>Eclipse 鍙婂叾鎻掍歡榪樺叿澶囧緢澶氬叾浠栫殑鐗規э紝鍙簲鐢ㄤ簬 Python 寮鍙戜腑錛屽 XML 緙栬緫鍣ㄣ乁ML 緙栬緫鍣紙涓嶈繃澶у鏁版槸浠?Java 浠g爜涓轟腑蹇冿級錛岃繕鏈夎祫婧愭帶鍒舵柟闈㈢殑鎻掍歡銆傜洰鍓?Eclipse 鎻掍歡绔欑偣涓婂垪鍑虹殑鎻掍歡鍑犱箮鏈?500 涓紙璇峰弬闃?<a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺備腑鐨勭浉鍏抽摼鎺ワ級銆傛垜灝嗙潃閲嶄粙緇嶄竴涓寰堝 Python 寮鍙戜漢鍛橀兘鐗瑰埆鏈夌敤鐨勬彃浠訛細Eclipse 鍙戣鐗堜腑宸茬粡鍖呮嫭鐨?CVS 鎻掍歡錛屼笉榪囦笉浼氳璁虹粏鑺傚唴瀹廣?</p> <p>Eclipse 涓寘鎷壒鎬т赴瀵岀殑闆嗘垚 CVS錛?/p> <ul> <li>鏀寔 SSH銆乸server銆乪xt 絳夎繛鎺ユ柟娉曘? </li> <li>鍩烘湰 CVS 鍛戒護鐨勬敮鎸侊細媯鍑洪」鐩佹彁浜ゅ彉鏇淬佹洿鏂般佸悜.cvsignore 涓鍔犳枃浠舵垨妯″紡絳夌瓑銆? </li> <li>鏂囦歡鍚堝茍鏌ョ湅銆? </li> <li>鍦ㄦ簮浠g爜鎺у埗涓疄鐜板鏂囦歡涓嶅悓涔嬪鐨勬瘮杈冦? </li> <li>欏圭洰鍚屾錛屽茍鐢ㄨ祫鏂欏簱鏄劇ず鍑哄垹闄ゅ拰鏂板鐨勫唴瀹廣?</li> </ul> <p>榪樺彲浠ラ氳繃鎻愪緵鍏朵粬鎻掍歡鏉ユ敮鎸佸叾浠栨簮浠g爜鎺у埗緋葷粺錛屽 ClearCase銆丼ubversion銆乂isual SourceSafe 絳夈?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="N1019B"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">鍦?Eclipse 涓嬌鐢?Python 鐨勪氦浜掑紡 shell</font> </span> </a> </p> <p>Python 瑙i噴鍣ㄦ敮鎸?Python 浠g爜鐨勪氦浜掑紡鎵ц銆傝繖縐嶆柟寮忓浜庤皟璇曚竴孌典唬鐮佹槸闈炲父鏈夌敤鐨勶紝鍥犱負涓嶇敤鎶婁唬鐮佹斁榪?Python 鑴氭湰涓茍鎵ц鑴氭湰浜嗐傚悓鏃訛紝Python 瑙i噴鍣ㄧ殑浜や簰妯″紡鍙互寰堝鏄撳湴闆嗘垚鍒?Eclipse 涓?</p> <p>瑕佸鍔犲 Python 浜や簰寮忔墽琛岀殑鏀寔錛屽彲閫氳繃 <b>Run > External Tools > External Tools</b> 澧炲姞涓涓?External Tool 鍚姩紼嬪簭銆傝繖鏃跺皢鎵撳紑 External Tool 鍚姩紼嬪簭閰嶇疆紿楀彛銆傚湪 Configurations 鍒楄〃涓夋嫨鈥淧rogram鈥濓紝鐒跺悗鐐瑰嚮鈥淣ew鈥濆垱寤轟竴涓柊鐨勯厤緗傚皢璇ラ厤緗懡鍚嶄負璇稿 "pythonInteractive" 涔嬬被錛岀劧鍚庤緗?Location錛屼護鍏舵寚鍚戞偍鐨?Python 瑙i噴鍣紝鎺ョ潃錛屽皢 "-i" 浣滀負鍞竴鐨勫弬鏁頒紶閫掕繘鏉ワ紙璇峰弬闃呭浘 7錛夈?</p> <p>鍦?Common 閫夐」鍗′笅錛岄変腑澶嶉夋錛屼嬌璇ラ厤緗湪 External Tools 鏀惰棌澶硅彍鍗曚腑鏄劇ず鍑烘潵銆?</p> <br /> <a name="N101AF"> <b>鍥?7. Python 浜や簰鏂瑰紡閰嶇疆</b> </a> <br /> <img alt="Python 浜や簰鏂瑰紡閰嶇疆" src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/pythonInteractiveConfig.jpg" /> <br /> <p>瑕佽繍琛屽垰鍒氬湪 Eclipse 涓垱寤虹殑鍚姩鍣紝鍙夋嫨 <b>Run > External Tools > pythonInterpreter</b>銆侾ython 瑙i噴鍣ㄧ殑杈撳嚭鏄劇ず鍦?Console 瑙嗗浘涓侰onsole 涓彲杈撳叆 Python 鍛戒護騫舵墽琛岋紝灝卞儚浠庡懡浠よ涓墽琛?Python 涓鏍楓備負瀵煎叆騫跺湪浜や簰妯″紡涓嬩嬌鐢ㄦā鍧楋紝鎮ㄩ渶瑕佸皢妯″潡鐨勪綅緗鍔犲埌 <code>PYTHONPATH</code> 鐜鍙橀噺涓?</p> <p>鍦?Eclipse Console 涓墽琛?Python 涓庣敤鍛戒護琛屾墽琛岀殑涓嶅悓涔嬪鍦ㄤ簬錛屾棤娉曞惎鐢ㄥ懡浠ゅ巻鍙茬壒鎬э紙閫氳繃鍚戜笂鍜屽悜涓嬬殑鏂瑰悜閿疄鐜幫級錛屽洜涓?Eclipse Console 浼氳嚜宸辮В閲婅繖浜涢敭銆?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="N101C9"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">鍦?Python 寮鍙戜腑浣跨敤 Ant</font> </span> </a> </p> <p>Python 浼氬湪瀹冮渶瑕佺殑鏃跺欒嚜鍔ㄧ紪璇戞ā鍧椼傝繖鎰忓懗鐫 Python 寮鍙戜漢鍛橀氬父涓嶅繀鏄懼紡鍦板妯″潡榪涜緙栬緫銆傚嵆渚垮姝わ紝鏈夋椂鍊欐墜宸ョ紪璇?Python 浠g爜榪樻槸寰堟湁鐢ㄧ殑錛屽悓鏃訛紝鏋勫緩鍜岄儴緗茶繃紼嬩腑榪樻湁寰堝鍏朵粬鏂歸潰鐨勫唴瀹瑰彲浠ヨ嚜鍔ㄥ寲瀹炵幇銆傝繖涔熸鏄瀯寤哄伐鍏風殑鐢ㄦ涔嬪湴銆?</p> <p>鎴戝皢鐫閲嶄粙緇嶆潵鑷?Java 緙栫▼涓栫晫涓殑 Apache Ant錛岃繖涓伐鍏峰彲澶ч噺搴旂敤鍦?Python 寮鍙戜腑銆侫pache Ant 鏄?Java 緙栫▼棰嗗煙鍐呬簨瀹炰笂鐨勬爣鍑嗘瀯寤哄伐鍏楓傚畠鏇村姞杞諱究錛屼笌 Java 鎶鏈粨鍚堝緱鏇村ソ錛屽彲鐢ㄤ簬鏇夸唬鍏朵粬鐨勬瀯寤哄伐鍏楓侫nt 鍙互鍦ㄦ敮鎸?Java 緙栫▼璇█鐨勪換浣曚竴縐嶅鉤鍙頒笂榪愯銆傚敖綆℃垜浠渶瑕佺殑澶у鏁版瀯寤虹壒鎬?Ant 閮藉凡緇忔彁渚涗簡錛屼絾濡傛灉瑕佸皢 Ant 鐢ㄥ仛 Python 鏋勫緩宸ュ叿錛岃繕鏄渶瑕佹湁涓浜涘叧閿殑涓?Python 鐩稿叧鐨勭壒鎬с傛垜宸茬粡寮鍙戜簡鑻ュ共瀹氬埗鐨?Ant 鎻掍歡錛堢敤 Ant 鐨勮璇濊鍙仛 task錛夛紝鍙彁渚涙瀯寤?Python 鏃墮渶瑕佺殑鐗瑰畾浜?Python 鐨勭壒鎬с?</p> <p>Ant 鐢?XML 浣滀負鎻忚堪鏋勫緩鐨勬牸寮忋俠uild 鏂囦歡緇勭粐涓洪渶瑕佹墽琛岀殑鐩爣銆傛瘡涓涓洰鏍囬兘鍙兘渚濊禆浜庡叾浠栫殑鐩爣銆侫nt 灝嗘牴鎹偍鎵璇鋒眰鎵ц鐨勭洰鏍囷紝浠ュ強涓緇勪緷璧栫洰鏍囷紝鏉ユ墽琛屼換浣曢渶瑕佺殑鐩爣銆傛瘡涓涓洰鏍囬兘鍙兘鍖呭惈浠繪剰鏁伴噺鐨?Ant 浠誨姟錛岃岀敱 Ant 浠誨姟瀹為檯鎵ц鐩爣鐨勫伐浣溿侫nt 鏈夊緢澶氬唴緗殑浠誨姟錛屽彲浠ュ畬鎴愯濡傜紪璇?Java 浠g爜銆佺敓鎴愭枃。銆佹搷綰墊枃浠跺拰鐩綍錛屽悓鏃剁涓夋柟鍙堟彁渚涗簡寰堝闄勫姞鐨勪換鍔°?</p> <p> <a name="N101D8"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">瀹夎 Python Ant 搴?/font> </strong> </span> </a> </p> <p>鎴戝皢閫氳繃涓?feedparser 欏圭洰鍒涘緩鏋勫緩鑴氭湰鏉ヤ粙緇?Ant 鏋勫緩鑴氭湰鍜?Python Ant 浠誨姟鐨勫熀紜鐭ヨ瘑銆備負浜嗕嬌鐢?Python Ant 浠誨姟錛屾偍闇瑕佷笅杞藉茍瀹夎鍖呭惈榪欎簺浠誨姟鐨?Java 搴撱傞鍏堬紝浠?<a ><font color="#996699">鍙傝冭祫鏂?/font></a> 涓鑺備腑鍒楀嚭鐨?URL 涓笅杞?Python Ant 浠誨姟搴擄紙pyAntTasks.jar錛夈傜劧鍚庯紝灝?JAR 鏂囦歡鎷瘋礉鍒?Eclipse 鐨?Ant 鎻掍歡涓嬬殑 lib 鐩綍涓傝繖搴旇鏄?Eclipse 瀹夎鐩綍涓嬪艦濡?plugins/org.apache.ant_1.5.3 鐨勫瓙鐩綍銆?</p> <p>Python Ant 浠誨姟搴撴嫹璐濆畬姣曚箣鍚庯紝蹇呴』鍦?Eclipse 涓惎鐢ㄥ簱銆傞夋嫨 <b>Window > Preferences</b>錛岀劧鍚庨夋嫨 <b>Ant > Runtime</b>銆傚皢 Ant Home Entries 灞曞紑錛屽叾涓彲鐪嬪埌 Eclipse 浣跨敤鐨勫簱錛圝AR 鏂囦歡錛夊垪琛ㄣ傞夋嫨鈥?Add JAR鈥濓紝鐒跺悗浠?Eclipse Ant 鎻掍歡鐨?lib 鐩綍涓夋嫨 Python Ant JAR 鏂囦歡錛屽氨鍙互灝嗗垰鍒氭嫹璐濈殑 Python Ant JAR 鏂囦歡鍔犲叆搴撳垪琛ㄤ腑錛堣鍙傞槄鍥?8錛夈?</p> <br /> <a name="N101F0"> <b>鍥?8. 鍚?classpath 涓姞鍏?Python Ant 浠誨姟</b> </a> <br /> <img alt="鍚?classpath 涓姞鍏?Python Ant 浠誨姟" src="http://www-128.ibm.com/developerworks/cn/opensource/os-ecant/addPyAntTasks.jpg" /> <br /> <p>鎮ㄧ幇鍦ㄥ簲璇ヨ兘澶熷垱寤哄拰榪愯鍖呭惈 Python 浠誨姟鐨?Ant 鏋勫緩鑴氭湰浜嗐備笅闈㈣繘鍏ユ瀯寤鴻剼鏈唴閮紒 </p> <p> <a name="N10200"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">鍒涘緩鏋勫緩鑴氭湰</font> </strong> </span> </a> </p> <p>鎴戝皢閫愭浠嬬粛濡備綍鍒涘緩涓涓畝鍗曠殑 Python 鏋勫緩鑴氭湰錛堣鍙傞槄娓呭崟 1錛夈傚畬鏁寸殑鏋勫緩鑴氭湰 build.xml 鍙粠 feedParserTest 欏圭洰鐨勯《灞傜洰褰曚腑鎵懼埌銆?</p> <br /> <a name="N10209"> <b>娓呭崟 1. 鐢ㄤ簬緙栬瘧 Python 婧愪唬鐮佺殑鏋勫緩鑴氭湰鐗囨柇</b> </a> <br /> <table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"> <tbody> <tr> <td> <pre> <code class="section"> <font face="Lucida Console"><project name="feedParserTest" default="compile"> <taskdef resource="pyAntTasks.properties"/> <property name="src.dir" value="src"/> <target name="compile"> <py-compile dir="${src.dir}" pythonpath="${src.dir}" optimize="0"/> </target> </project> </font> </code> </pre> </td> </tr> </tbody> </table> <br /> <p>鍏堜粙緇嶄竴涓彧緙栬瘧 Python 鏍蜂緥浠g爜鐨勬瀯寤鴻剼鏈?lt;project> 鏍囩鎬繪槸鏋勫緩鑴氭湰鐨勬牴鏍囩銆?lt;taskdef> 鏍囩澹版槑鍦ㄦ暣涓瀯寤鴻剼鏈腑浣跨敤鐨?Python 浠誨姟銆傚湪鏋勫緩鑴氭湰鐨勫簳閮紝鍙互瀹氫箟 <code>compile</code> 鐩爣銆傜洰鏍囧厓绱犲唴閮ㄦ槸 <code>compile</code> 榪愯鏈熼棿鎵ц鐨勪換鍔°傜壒鍒殑鏄?py-compile 浠誨姟錛屽畠璐熻矗浠?src 鐩綍寮濮嬶紝緙栬瘧鎵鏈夌殑 Python 浠g爜銆傝浠誨姟浼氶掑綊閬嶅巻鎵鏈夌殑瀛愮洰褰曪紝騫剁紪璇戞墍鏈夌殑 Python 妯″潡銆傝剼鏈腑娌℃湁閲囩敤灝?src 鐩綍紜紪鐮佸埌璋冪敤涔嬪鐨勬柟寮忥紝鑰屾槸鍦ㄦ瀯寤鴻剼鏈腑瀹氫箟浜嗙О涓?src.dir 鐨勫睘鎬с傜劧鍚庯紝鍦ㄩ渶瑕佷嬌鐢ㄨ繖涓洰褰曞悕鐨勬椂鍊欙紝灝卞彲浠ラ氳繃 <code>${src.dir}</code> 鏉ュ紩鐢ㄣ?</p> <p>瑕佽繍琛屾瀯寤鴻剼鏈紝鍙粠 Eclipse 涓墦寮瀹冦侲clipse 鍏鋒湁鍐呯疆鐨?Ant 鏋勫緩鑴氭湰緙栬緫鍜屾祻瑙堝姛鑳姐侽utline 瑙嗗浘鍙互鏄劇ず鍑烘瀯寤鴻剼鏈殑緇撴瀯銆傚湪 Navigator 瑙嗗浘涓紝閫夋嫨璇ユ瀯寤鴻剼鏈紝鐢ㄥ彸閿偣鍑伙紝鐒跺悗閫夋嫨鈥淩un Ant...鈥濄傞夋嫨 <code>compile</code> 鐩爣錛岀劧鍚庣偣鍑燴淩un鈥濄傛瀯寤鴻剼鏈墽琛岃繃紼嬩腑鐨勮緭鍑轟俊鎭簲璇ユ樉紺哄湪 Console 瑙嗗浘涓紝琛ㄧず榪愯鎴愬姛銆?</p> <p> <a name="N10229"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Python 鑴氭湰鎵ц浠誨姟</font> </strong> </span> </a> </p> <p>鎺ヤ笅鏉ュ皢鍚戞瀯寤鴻剼鏈腑鍔犲叆鏂扮殑鐩爣錛岀敤浜庢墽琛?Python 鑴氭湰錛堣鍙傞槄娓呭崟 2錛夈傚湪鏈緥涓紝鍙互灝?RSS URL 浣滀負鍙傛暟鏉ユ墽琛?feedparser.py 鑴氭湰銆?</p> <br /> <a name="N10232"> <b>娓呭崟 2. 榪愯 feedparser 鑴氭湰鐨勬瀯寤鴻剼鏈墖鏂?/b> </a> <br /> <table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"> <tbody> <tr> <td> <pre> <code class="section"> <font face="Lucida Console"> <target name="run.feedparser" depends="compile"> <py-run script="src/feedparser/feedparser.py" pythonpath="${src.dir}" optimize="0"> <arg value="http://www.ibm.com/developerworks/news/dw_dwtp.rss"> </py-run> </target> </font> </code> </pre> </td> </tr> </tbody> </table> <br /> <p>涓婇潰鐨勭洰鏍囦互 RSS URL 涓哄敮涓鐨勫弬鏁版潵鎵ц feedparser.py 鑴氭湰銆傝鐩爣澹版槑涓轟緷璧栦簬 <code>compile</code> 鐩爣錛屾墍浠ュ悗鑰呭皢棣栧厛鎵ц銆傚疄闄呬笂榪欎竴姝ュ茍涓嶆槸寰堝繀瑕侊紝鍥犱負 Python 浼氭牴鎹渶瑕佽嚜鍔ㄧ紪璇戞簮浠g爜銆傚鏋滄偍鎵ц <code>run.feedparser</code> 鐩爣錛屽氨浼氳繍琛?feedparser.py 鑴氭湰錛屽悓鏃跺皢 RSS 鐨勫唴瀹硅緭鍑哄埌 Console 涓?</p> <p> <a name="N10247"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Python 鏂囨。浠誨姟</font> </strong> </span> </a> </p> <p>Python 鐨?API 鏂囨。緙栧埗鏈哄埗涓?Java 鎶鏈腑鐨?JavaDoc 緋葷粺綾諱技錛岀О涓?PyDoc銆傚湪鏋勫緩鑴氭湰涓姞鍏ユ竻鍗?3 涓垪鍑虹殑濡備笅 XML 鐗囨柇錛屽彲涓烘墍鏈夌殑 Python 妯″潡鐢熸垚 PyDoc銆?</p> <br /> <a name="N10250"> <b>娓呭崟 3. 鐢ㄤ簬鐢熸垚 PyDoc 鐨勬瀯寤鴻剼鏈墖鏂?/b> </a> <br /> <table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"> <tbody> <tr> <td> <pre> <code class="section"> <font face="Lucida Console"> 1: <property name="pydoc.dir" value="pydoc"/> 2: 3: <target name="init"> 4: <mkdir dir="${pydoc.dir}"/> 5: </target> 6: 7: <target name="pydoc" depends="init,compile"> 8: <py-doc pythonpath="${src.dir}" destdir="${pydoc.dir}"> 9: <fileset dir="${src.dir}"> 10: <include name="**/*"/> 11: </fileset> 12: </py-doc> 13: </target> </font> </code> </pre> </td> </tr> </tbody> </table> <br /> <p> <code>浠庡涓婅堪 pydoc 鐩爣鐨勮В鏋愬彲鐪嬪嚭錛岀 7 琛屽0鏄庝簡鐩爣鍚嶇О錛屽茍鎸囧嚭瀹冧緷璧栦簬 <code>init</code> 鍜?<code>compile</code> 鐩爣銆傝繖鎰忓懗鐫鍦ㄨ繍琛?pydoc 鐩爣涔嬪墠錛孉nt 蹇呴』淇濊瘉 <code>init</code> 鍜?<code>compile</code> 鐩爣宸茬粡榪愯錛屽鏋滄病鏈夛紝鍒欓鍏堣繍琛岃繖涓や釜鐩爣銆?</code> </p> <p> <code>pydoc</code> 鐩爣鎵渚濊禆鐨?<code>init</code> 鐩爣鍦ㄧ 3 鑷崇 5 琛屽畾涔夈?<code>init</code> 鐩爣浠呬粎鍒涘緩浜嗕竴涓瓨鏀?PyDoc API 鏂囨。鏂囦歡鐨勭洰褰曘傚鍓嶆墍榪幫紝瑕佷負鎵鐢熸垚鏂囨。鐨勪繚瀛樹綅緗畾涔変竴涓睘鎬э紝鍚嶄負 pydoc.dir銆?</p> <p>絎?8 琛屽紑濮嬫槸 py-doc 浠誨姟銆傚鍓嶆墍榪幫紝鎮ㄤ紶鍏ョ敓鎴?pydoc 榪囩▼涓墍浣跨敤鐨?<code>PYTHONPATH</code> 銆?<code>destdir</code> 灞炴у憡璇?py-doc 浠誨姟灝嗙敓鎴愮殑 HTML 鏂囨。杈撳嚭鍒頒綍澶勩?</p> <p>絎?9 鑷崇 11 琛屽畾涔変簡鍦ㄧ敓鎴愭枃。鐨剺q囩▼涓簲璇ュ鐞嗗摢浜?Python 婧愭枃浠躲傛枃浠墮泦鏄?Ant 鑴氭湰涓氱敤鐨勭粨鏋勶紝鍙敤浜庡畾涔夋墍鎿嶄綔鐨勪竴緇勬枃浠躲傝繖鏄竴縐嶅緢寮哄ぇ鐨勭壒鎬э紝瀹冧嬌鎮ㄨ兘澶熼氳繃鍚嶅瓧妯″紡銆佸竷灝旈昏緫鍜屾枃浠跺睘鎬ф潵閫夋嫨鎵瑕佹搷浣滅殑鏂囦歡銆侫nt 鏂囨。涓湁榪欐柟闈㈢殑瀹屾暣鎻忚堪銆傛湰渚嬩腑閫掑綊閫夋嫨浜嗏渟rc鈥濈洰褰曚笅鐨勬墍鏈夋枃浠躲?</p> <p> <a name="N1028D"> <span id="hvzpftn" class="smalltitle"> <strong> <font face="Arial">Python 鍗曞厓嫻嬭瘯浠誨姟</font> </strong> </span> </a> </p> <p>Python 涓叿鏈夋爣鍑嗙殑鍗曞厓嫻嬭瘯妗嗘灦錛堜粠 Python 2.3 寮濮嬨傚湪 Python 2.2 涓繖鍙槸鍙夋ā鍧楋級錛屼笌 Java jUnit 妗嗘灦鍗佸垎綾諱技銆傛祴璇曠敤渚嬬殑緇撴瀯涓?jUnit 閲囩敤鐩稿悓鐨勬柟寮忋傛瘡涓涓緟嫻嬭瘯鐨勭被鍜屾ā鍧楅氬父閮藉叿鏈夎嚜宸辯殑嫻嬭瘯綾匯傛祴璇曠被涓寘鍚祴璇曡緗紙fixture錛夛紝瀹冧滑鍦?<code>setUp</code> 鍑芥暟涓垵濮嬪寲銆傛瘡涓涓祴璇曢兘緙栧啓涓烘祴璇曠被涓殑涓涓嫭绔嬬殑嫻嬭瘯鍑芥暟銆倁nittest 妗嗘灦浼氬湪嫻嬭瘯鍑芥暟涔嬮棿寰幆寰澶嶏紝鍏堣皟鐢?<code>setUp</code> 銆佸啀嫻嬭瘯鍑芥暟銆佺劧鍚庢竻闄わ紙 <code>tearDown</code> 錛夋祴璇曞嚱鏁般傝鍙傞槄娓呭崟 4 涓殑鏍蜂緥銆?</p> <br /> <a name="N102A2"> <b>娓呭崟 4. Python 鍗曞厓嫻嬭瘯妯″潡</b> </a> <br /> <table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"> <tbody> <tr> <td> <pre> <code class="section"> <font face="Lucida Console">import unittest from pprint import pprint import feedparser class FeedparserTest(unittest.TestCase): """ A test class for the feedparser module. """ def setUp(self): """ set up data used in the tests. setUp is called before each test function execution. """ self.developerWorksUrl = "testData/developerworks.rss" def testParse09Rss(self): """ Test a successful run of the parse function for a 0.91 RSS feed. """ print "FeedparserTest.testParse09RSS()" result = feedparser.parse(self.developerWorksUrl) pprint(result) self.assertEqual(0, result['bozo']) self.assert_(result is not None) channel = result['channel'] self.assert_(channel is not None) chanDesc = channel['description'] self.assertEqual(u'The latest content from IBM developerWorks', chanDesc) items = result['items'] self.assert_(items is not None) self.assert_(len(items)> 3) firstItem = items[0] title = firstItem['title'] self.assertEqual(u'Build installation packages with solution installation and deployment technologies', title) def tearDown(self): """ tear down any data used in tests tearDown is called after each test function execution. """ pass if __name__ == '__main__': unittest.main() </font> </code> </pre> </td> </tr> </tbody> </table> <br /> <p>涓婅堪娓呭崟鏄疄鐜?feedparser 妯″潡鍩烘湰嫻嬭瘯鍔熻兘鐨勬祴璇曠被銆傚畬鏁寸殑嫻嬭瘯綾昏 feedParserTest 欏圭洰涓嬬殑 src/feedparserTest/FeedparserTest.py銆?<code>setUp</code> 鍑芥暟璐熻矗鍑嗗鏁翠釜嫻嬭瘯榪囩▼涓渶瑕佷嬌鐢ㄧ殑嫻嬭瘯瑁呯疆錛屽湪鏈緥涓彧鏈夋祴璇曠敤鐨?RSS 鏂囦歡鐨勭洰褰曪紝嫻嬭瘯鍑芥暟灝嗗鍏惰繘琛岃В鏋愩?<code>testParse09Rss</code> 鏄湡姝g殑嫻嬭瘯鍑芥暟銆傝繖涓嚱鏁拌皟鐢?feedparser.parse 鍑芥暟錛屼紶閫掓祴璇曠敤鐨?RSS 鏂囦歡錛岃緭鍑鴻В鏋愮粨鏋滐紝騫墮氳繃 TestCase 綾葷殑 assert 鍑芥暟鎵ц鍩烘湰鐨勬鏌ョ粺浣溿傚鏋滀換浣?assert 鐨勬眰鍊肩粨鏋滀笉鏄湡錛屾垨鏄湪鎵ц榪囩▼涓姏鍑轟換浣曞紓甯革紝unittest 灝變細鎶ュ憡涓嬈℃祴璇曞け璐ユ垨閿欒銆傛渶鍚庣殑涓よ璐熻矗鍦ㄨ繖涓祴璇曠被鍐呴儴榪愯嫻嬭瘯錛屾柟娉曟槸鐩存帴榪愯璇ユā鍧楀嵆鍙?</p> <p>瑕佺嫭绔嬭繍琛岃嫻嬭瘯綾伙紝鍙互鎸夊墠闈㈡墍璇寸殑鐩稿悓鏂瑰紡榪愯 FeedparserTest.py 妯″潡銆傚湪 Eclipse Navigator 瑙嗗浘涓夋嫨 FeedparserTest.py錛岀劧鍚庨氳繃 <b>Python > Run</b> 榪愯銆傛鏃舵樉紺哄惎鍔ㄩ厤緗獥鍙c傞櫎 Base 鐩綍涔嬪錛屽叾浠栭兘淇濇寔緙虹渷鍊煎嵆鍙侭ase 鐩綍蹇呴』鏄?feedParserTest 欏圭洰鐨勭洰褰曪紝榪欐牱鎵嶈兘鍦ㄥ綋鍓嶇洰褰曚笅鎵懼埌 RSS 鏂囦歡錛坱estData/developerworks.rss錛夈備慨鏀?base 鐩綍鐨勮緗紝鐒跺悗鐐瑰嚮鈥淩un鈥濄傝緭鍑轟俊鎭樉紺哄湪 Console 涓娿?</p> <p>鎮ㄤ篃璁稿笇鏈涙垜浠紪鍐欑殑鎵鏈夊崟鍏冩祴璇曢兘鑳藉浣滀負鏋勫緩鐨勪竴閮ㄥ垎鑷姩鎵ц銆傚皢涓嬮潰娓呭崟 5 鎵紺虹殑鏋勫緩鐗囨柇鍔犲叆鏋勫緩鑴氭湰渚垮彲瀹炵幇銆?</p> <br /> <a name="N102C0"> <b>娓呭崟 5. 鎵ц鍗曞厓嫻嬭瘯鐨勬瀯寤鴻剼鏈墖鏂?/b> </a> <br /> <table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"> <tbody> <tr> <td> <pre> <code class="section"> <font face="Lucida Console">1: <target name="tests" depends="compile"> 2: <py-test pythonpath="${src.dir}" dir="."> 3: <fileset dir="${src.dir}"> 4: <include name="**/*Test.py"/> 5: </fileset> 6: </py-test> 7: </target> </font> </code> </pre> </td> </tr> </tbody> </table> <br /> <p>絎竴琛屾槸鐩爣澹版槑錛岃繖涓庡叾浠栫殑鑴氭湰鐩稿悓銆傜 2 鑷崇 6 琛岃皟鐢?py-test 浠誨姟銆傝繖閮ㄥ垎浠g爜灝嗗湪鈥渟rc鈥濈洰褰曚笅鏌ユ壘鎵鏈変互鈥淭est.py鈥濈粨灝劇殑鎵鏈夋枃浠訛紝騫惰繍琛屾墍鏈夋祴璇曘?<code>PYTHONPATH</code> 璁劇疆涓衡渟rc鈥濓紝嫻嬭瘯鎵ц鐨勫綋鍓嶅伐浣滅洰褰曞氨鏄綋鍓嶇洰褰曪紙鈥?鈥欙級銆?</p> <p>榪愯鐩爣鐨勬柟娉曟槸鍏堣繍琛屾瀯寤鴻剼鏈紝鍐嶉夋嫨鎵ц鈥渢ests鈥濈洰鏍囥傝鐩爣灝嗚繍琛屾墍鏈変互鈥淭est.py鈥濈粨灝劇殑嫻嬭瘯鐢ㄤ緥錛屾湰渚嬩腑浠呮湁 FeadparserTest.py銆?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font face="Verdana" color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="N102D4"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">緇撴潫璇?/font> </span> </a> </p> <p>Eclipse 鍜?PyDev 鎻掍歡鐨勭粨鍚堬紝浠ュ強 Apache Ant 涓?Python Ant 浠誨姟涓璧蜂嬌鐢紝鍙互涓?Python 寮鍙戞彁渚涘畬鍏ㄩ泦鎴愮殑寮鍙戠幆澧冨拰鏋勫緩/閮ㄧ講宸ュ叿銆傝繖浜涘伐鍏峰皻鍦ㄥ紑鍙戣繃紼嬩腑錛屽洜姝よ緇忓父鏌ョ湅鏄惁鏈夋洿鏂幫紝濡傛灉鎮ㄨ寰楃壒鍒笇鏈涚湅鍒版煇縐嶇壒鎬э紝鍙互鍗瘋搗琚栫鑷姏鏇寸敓銆?</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font face="Verdana" color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="resources"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">鍙傝冭祫鏂?</font> </span> </a> </p> <ul> <li>鎮ㄥ彲浠ュ弬闃呮湰鏂囧湪 developerWorks 鍏ㄧ悆绔欑偣涓婄殑 <a ><font color="#5c81a7">鑻辨枃鍘熸枃</font></a>. <br /><br /></li> <li>涓嬭澆鏈枃鐨?<a href="ftp://www6.software.ibm.com/software/developer/library/os-ecant/feedparsertest.zip"><font color="#996699">鏍蜂緥紼嬪簭涓庢簮浠g爜</font></a>銆?<br /><br /></li> <li>璁塊棶 <a ><font color="#996699">Eclipse Web 绔欑偣</font></a>鑾峰彇 Eclipse 涓嬭澆鏂囦歡鍜?Eclipse 鏂囨。銆?<br /><br /></li> <li> <a > <font color="#5c81a7">Eclipse Plug-ins Web 绔欑偣</font> </a>涓寘鍚暟浠ョ櫨璁$殑鍟嗕笟鍜屽紑鏀炬簮浠g爜鎻掍歡錛屽彲渚涘湪 Eclipse 涓畨瑁呫?<br /><br /></li> <li> <a > <font color="#5c81a7">Apache Ant Web 绔欑偣</font> </a>涓婃湁 Ant 涓嬭澆鍜屾湁鍏?Ant 鐨勬枃。銆?<br /><br /></li> <li>瑕佽幏寰?Python 鐨勪笅杞藉拰鐩稿叧淇℃伅錛岃鍙傞槄 <a ><font color="#5c81a7">Python Web 绔欑偣</font></a>銆?<br /><br /></li> <li> <a > <font color="#5c81a7">PyDev SourceForge 欏圭洰绔欑偣</font> </a>鎻愪緵浜嗕笅杞姐佹枃。鍜岀敤鎴疯鍧涖?<br /><br /></li> <li> <a > <font color="#5c81a7">Python Ant tasks Web 绔欑偣</font> </a>鍖呭惈鏈鏂扮増鏈殑 Python Ant 浠誨姟銆?<br /><br /></li> <li>Feed Parser 鏄敤 Python 瀹炵幇鐨勯氱敤璧勬簮瑙f瀽鍣紝鍙粠 <a ><font color="#5c81a7">Feed Parser Web 绔欑偣</font></a>涓婃壘鍒般?<br /><br /></li> <li> <a > <font color="#5c81a7"> <i>Python in a Nutshell</i> </font> </a>錛圤'Reilly & Associates錛?003錛夛紝浣滆?Alex Martelli錛屾槸浼樼鐨?Python 璇█鍙傝冩墜鍐屻?<br /><br /></li> <li>鍦?<i>developerWorks</i>鐨?<a ><font color="#5c81a7">寮鏀炬簮浠g爜欏圭洰涓撳尯</font></a>涓彲浠ユ壘鍒版洿澶?<a ><font color="#5c81a7">涓?Eclipse 鐢ㄦ埛鎾板啓鐨勬枃绔?/font></a>銆備篃鍙互鍙傜湅 <i>alphaWorks</i>涓婃渶鏂扮殑 <a ><font color="#5c81a7">Eclipse 鎶鏈笅杞?/font></a>銆?<br /><br /></li> <li>鍦?Developer Bookstore 鐨勫紑鏀炬簮浠g爜鍖轟腑錛屽彲浠ユ壘鍒版暟鐧炬湰 <a ><font color="#5c81a7">寮鏀炬簮浠g爜鎵撴姌涔︾睄</font></a>錛屽叾涓寘鎷嚑鏈?<a ><font color="#5c81a7">Eclipse</font></a>鍜?<a ><font color="#5c81a7">Struts 搴旂敤紼嬪簭寮鍙?/font></a> 鏂歸潰鐨勫浘涔︺?<br /><br /></li> <li>閫氳繃 <a target="new"><font color="#5c81a7">developerWorks Subscription</font></a>鐢ㄦ渶鏂扮殑 IBM 宸ュ叿鍜屼腑闂翠歡寮鍙戝強嫻嬭瘯鎮ㄧ殑搴旂敤紼嬪簭錛涙偍鍙互鑾峰緱鍖呮嫭鍩轟簬 Eclipse 鐨?WebSphere銆丏B2銆丩otus銆丷ational銆乀ivoli 鍦ㄥ唴鐨?IBM 杞歡錛屽悓鏃惰繕鏈夊彲浣跨敤 12 涓湀鐨勮蔣浠惰鍙紝鐪侀挶瓚呭嚭鎮ㄧ殑鎯寵薄銆?<br /><br /></li> <li>涓嬭澆 developerWorks Subscription 綺鵑変駭鍝佺殑鍏嶈垂璇曠敤鐗堬紝榪欎簺浜у搧鍙繍琛屼簬 Linux 涓婏紝鍖呮嫭 WebSphere Studio Site Developer銆乄ebSphere SDK for Web services銆乄ebSphere Application Server銆丏B2 Universal Database Personal Developers Edition銆乀ivoli Access Manager 浠ュ強 Lotus Domino Server銆備笅杞介摼鎺ュ湪 developerWorks 鐨?<a ><font color="#5c81a7">Speed-start your Linux app</font></a>鍖恒傛兂鏇村揩寮濮嬩箞錛熻鑷繁璁塊棶浜у搧浣跨敤鏂囨。鍜屾妧鏈敮鎸併?<br /></li> </ul> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /> <br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /> </td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td> <img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> <br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="center"> <img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /> <br /> </td> <td valign="top" align="right"> <a class="fbox" > <b> <font face="Verdana" color="#996699">鍥為〉棣?/font> </b> </a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p> <a name="author"> <span id="hvzpftn" class="atitle"> <font face="Arial" size="4">鍏充簬浣滆?/font> </span> </a> </p> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td colspan="3"> <font face="Arial" size="4"> <img height="5" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /> </font> </td> </tr> <tr valign="top" align="left"> <td> <p> <font face="Arial" size="4"> </font> </p> </td> <td> <font face="Arial" size="4"> <img height="5" alt="" src="http://www.ibm.com/i/c.gif" width="4" /> </font> </td> <td width="100%"> <p>Ron Smith 鏄?RPS Technologies, Inc 鐨勫垱濮嬩漢銆傝繖鏄竴瀹惰蔣浠跺紑鍙戜笌杞歡欏鵑棶鍏徃錛屾婚儴浣嶄簬鑺濆姞鍝ュ湴鍖恒俁on Smith 涓哄鎴鋒彁渚涘熀浜?J2EE 鐨勪紒涓氬簲鐢ㄧ▼搴忓彂鏂歸潰鐨勫挩璇紝鍚屾椂涔熷湪 RPS Technologies 鍐呴儴寮鍙戣蔣浠朵駭鍝併傚彲浠ラ氳繃 <a href="mailto:ron.smith@rpstechnologies.net"><font color="#5c81a7">ron.smith@rpstechnologies.net</font></a>涓?Ron 鑱旂郴銆?</p> </td> </tr> </tbody> </table> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5521.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-14 11:34 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/14/5521.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python 瀛︿範涔嬭礬錛堝洓錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5494.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Thu, 13 Apr 2006 13:49:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5494.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5494.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5494.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5494.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5494.html</trackback:ping><description><![CDATA[ <p> <a name="c40ab4c10b2"> <font face="Arial" size="2"> </font> </a> <b> </b> </p> <p> <font face="Arial"> <font size="2"> <strong>Note One錛歛bout sequences and lists<br /></strong> <br />搴忓垪鏄笉鍙彉鍒楄〃銆備竴鏃﹀垱寤轟簡涓涓簭鍒楀氨涓嶈兘浠ヤ換浣曟柟寮忔敼鍙樺畠銆?/font> </font> </p> <p> <a name="c40ab4c10b3"> </a> <b> <font face="Arial" size="2">渚?1.21. 瀹氫箟搴忓垪<br /></font> </b> <font face="Arial"> <font size="2"> <tt> <br />>>> </tt> <span id="hvzpftn" class="userinput">t = (<tt class="string">"a"</tt>, <tt class="string">"b"</tt>, <tt class="string">"mpilgrim"</tt>, <tt class="string">"z"</tt>, <tt class="string">"example"</tt>)</span> </font> </font> <a name="odbchelper.tuple.1.1"> </a> <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">t</span> <br /> <span id="hvzpftn" class="computeroutput">('a', 'b', 'mpilgrim', 'z', 'example')</span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">t[0]</span> </font> <a name="odbchelper.tuple.1.2"> </a> <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> <br /> <span id="hvzpftn" class="computeroutput">'a'</span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">t[-1]</span>聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 </font> <a name="odbchelper.tuple.1.3"> </a> <font face="Arial" size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> <br /> <span id="hvzpftn" class="computeroutput">'example'</span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">t[1:3]</span>聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 </font> <a name="odbchelper.tuple.1.4"> </a> <font face="Arial"> <font size="2"> <img height="12" alt="4" src="http://cn.diveintopython.org/images/callouts/4.jpg" width="12" border="0" /> <br /> <span id="hvzpftn" class="computeroutput">('b', 'mpilgrim')</span> </font> </font> </p> <table summary="Callout list" border="0"> <tbody> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" color="#ff0000" size="2">搴忓垪鐨勫畾涔夊悓鍒楄〃鐨勫畾涔夋柟寮忕浉鍚岋紝闄や簡鏁翠釜鍏冪礌闆嗘槸鐢ㄥ皬鎷彿鍖呭洿鐨勮屼笉鏄柟鎷彿銆?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" size="2">搴忓垪鐨勫厓绱犺薄鍒楄〃涓鏍鋒寜瀹氫箟鐨勬搴忚繘琛屾帓搴忋傚簭鍒楃殑绱㈠紩璞″垪琛ㄤ竴鏍蜂粠0寮濮嬶紝鎵浠ヤ竴涓潪絀哄簭鍒楃殑絎竴涓厓绱犳繪槸<tt> t[0]</tt>銆?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" size="2">璐熸暟绱㈠紩璞″垪琛ㄤ竴鏍蜂粠搴忓垪灝鵑儴寮濮嬭鏁般?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="4" src="http://cn.diveintopython.org/images/callouts/4.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" size="2">鍒嗙墖涔熷彲浠ヤ嬌鐢紝灝辮薄鍒楄〃涓鏍楓傛敞鎰忓綋鍒嗗壊涓涓垪琛ㄦ椂錛屼細寰楀埌涓涓柊鐨勫垪琛紱褰撳垎鍓蹭竴涓簭鍒楁椂錛屼細寰楀埌涓涓柊鐨勫簭鍒椼?/font> </td> </tr> </tbody> </table> <p> <a name="odbchelper.tuplemethods"> </a> <b> <font face="Arial" size="2">渚?1.22. 搴忓垪娌℃湁鏂規硶</font>聽<br /></b> <font face="Arial"> <font size="2"> <br /> <tt>>>>聽</tt> <span id="hvzpftn" class="userinput">t</span> <br /> <span id="hvzpftn" class="computeroutput">('a', 'b', 'mpilgrim', 'z', 'example')</span> <br /> <tt>>>>聽</tt> <span id="hvzpftn" class="userinput">t.append(<tt class="string">"new"</tt>)</span>聽</font>聽<br /></font> <font face="Arial"> <font size="2"> <span id="hvzpftn" class="traceback">Traceback (innermost last):<br />File "<interactive input>", line 1, in ?<br />AttributeError: 'tuple' object has no attribute 'append'</span> <br /> <tt>>>>聽</tt> <span id="hvzpftn" class="userinput">t.remove(<tt class="string">"z"</tt>)</span>聽聽</font> </font> <font face="Arial" size="2">聽聽聽聽聽聽聽聽聽</font>聽聽聽聽<br /><font face="Arial"><font size="2"><span id="hvzpftn" class="traceback">Traceback (innermost last):<br />File "<interactive input>", line 1, in ?<br />AttributeError: 'tuple' object has no attribute 'remove'</span><br /><tt>>>>聽</tt><span id="hvzpftn" class="userinput">t.index(<tt class="string">"example"</tt>)</span>聽聽<br /></font></font><font face="Arial"><font size="2"><span id="hvzpftn" class="traceback">Traceback (innermost last):<br />聽 File "<interactive input>", line 1, in ?<br />AttributeError: 'tuple' object has no attribute 'index'</span><br /><tt>>>></tt><span id="hvzpftn" class="userinput"><tt class="string">"z"</tt>聽<tt class="keyword">in</tt> t</span>聽聽<br /></font></font><font face="Arial"><font size="2"><span id="hvzpftn" class="computeroutput">1</span>聽聽聽</font></font></p> <table summary="Callout list" border="0"> <tbody> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" color="#ff0000" size="2">浣犱笉鑳藉悜搴忓垪澧炲姞鍏冪礌銆傚簭鍒楁病鏈?<font face="Arial"><tt>append</tt> 鎴?</font><tt>extend</tt> 鏂規硶銆?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" color="#ff0000" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> </font> </a> <font face="Arial" color="#ff0000" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" color="#ff0000" size="2">浣犱笉鑳戒粠搴忓垪涓櫎鎺夊厓绱犮傚簭鍒楁病鏈?<tt>remove</tt> 鎴?tt> pop</tt> 鏂規硶銆?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" color="#ff0000" size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> </font> </a> <font face="Arial" color="#ff0000" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" color="#ff0000" size="2">浣犱笉鑳藉湪搴忓垪涓煡鎵懼厓绱犮傚簭鍒楁病鏈?<tt>index</tt> 鏂規硶銆?/font> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" color="#ff0000" size="2"> <img height="12" alt="4" src="http://cn.diveintopython.org/images/callouts/4.jpg" width="12" border="0" /> </font> </a> <font face="Arial" color="#ff0000" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" size="2"> <font color="#ff0000">鐒惰岋紝浣犲彲浠ヤ嬌鐢?<tt>in</tt> 鏉ョ湅涓鐪嬫槸鍚︿竴涓厓绱犲瓨鍦ㄤ簬搴忓垪涓?/font>銆?/font> </td> </tr> </tbody> </table> <p> <font face="Arial" size="2">閭d箞搴忓垪鏈変粈涔堝ソ澶勫憿錛?/font> </p> <ul> <li> <font face="Arial" size="2"> <font color="#ff0000">搴忓垪姣斿垪琛ㄦ搷浣滈熷害蹇傚鏋滀綘瀹氫箟浜嗕竴涓奸泦鍚堝父閲忥紝騫朵笖鍞竴瑕佺敤瀹冨仛鐨勬槸涓嶆柇鍦伴亶鍘嗗畠錛屼嬌鐢ㄥ簭鍒椾唬鏇垮垪琛ㄣ?/font> </font> </li> <li> <font face="Arial" size="2">璁板緱鎴戣榪?/font> <a title="Example 1.11. Mixing datatypes in a dictionary" > <font face="Arial" size="2">瀛楀吀鍏抽敭瀛?/font> </a> <font face="Arial" size="2">鍙互鏄暣鏁幫紝瀛楃涓插拰鈥滃嚑縐嶅叾瀹冪殑綾誨瀷鈥濆悧錛熷簭鍒楀氨鏄偅浜涚被鍨嬩箣涓銆傚簭鍒楀彲浠ュ湪瀛楀吀涓鐢ㄤ綔鍏抽敭瀛楋紝浣嗘槸鍒楄〃涓嶈銆?sup>[<a name="c40ab4c10b6b1ab5">2</a>]</sup></font> </li> <li> <font face="Arial" size="2">搴忓垪鐢ㄥ湪瀛楃涓叉牸寮忓寲錛岃繖涓鐐規垜浠細寰堝揩鐪嬪埌銆?</font> </li> </ul> <p align="left"> <font size="2"> <font face="Arial"> <strong>Note Two : Mapping in the Lists</strong> <br /> <br /> <strong>鍒楄〃鏄犲皠浠嬬粛</strong>聽</font> </font>聽聽聽聽<font size="2"><font face="Arial">聽聽<br /><tt>>>>聽</tt>聽<span id="hvzpftn" class="userinput">li = [1, 9, 8, 4]</span><br /><tt>>>>聽</tt>聽<span id="hvzpftn" class="userinput">[elem*2 <tt class="keyword">for</tt> elem <tt class="keyword">in</tt> li]</span></font></font><font face="Arial" size="2">聽聽聽聽聽 <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /><br /><span id="hvzpftn" class="computeroutput">[2, 18, 16, 8]</span><br /><tt>>>>聽</tt>聽<span id="hvzpftn" class="userinput">li聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span></font><font size="2"><font face="Arial"><img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /><br /><span id="hvzpftn" class="computeroutput">[1, 9, 8, 4]</span></font></font></p> <p> </p> <table summary="Callout list" border="0"> <tbody> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">涓轟簡瀵硅繖涓鐐規湁涓涓劅鎬ц璇嗭紝浠庡彸鍚戝乏鐪嬪畠銆?<tt>li</tt> 鏄竴涓皢瑕佹槧灝勭殑鍒楄〃銆侾ython寰幆閬嶅巻 <tt>li</tt> 涓嬈′竴涓厓绱狅紝涓存椂灝嗘瘡涓厓绱犵殑鍊艱祴緇欏彉閲?<tt>elem</tt>銆傜劧鍚嶱ython浣跨敤鍑芥暟 <tt><tt>elem</tt>*2</tt> 錛屾帴鐫灝嗙粨鏋滆拷鍔犲埌榪斿洖鍒楄〃涓?/font> </p> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> </font> </a> <font face="Arial" color="#a52a2a" size="2"> </font> </td> <td valign="top" align="left"> <font face="Arial" color="#a52a2a" size="2">娉?font color="#ff0000">鎰忓垪琛ㄦ槧灝勪笉鏀瑰彉琚槧灝勭殑鍒楄〃銆?/font></font> </td> </tr> </tbody> </table> <br /> <font face="Arial" size="2"> <strong>Note Three :濂戒笢瑗柯爈ambda聽鍑芥暟</strong> </font> <p> <a name="c40ab5b9b2"> </a> <b> </b> </p> <p> <font face="Arial" size="2">Python鏀寔涓縐嶆湁瓚g殑璇硶錛屽畠鍏佽浣犲揩閫熷畾涔夊崟琛岀殑鏈灝忓嚱鏁般傝繖浜涘彨鍋?<tt>lambda</tt> 鐨勫嚱鏁版槸浠嶭isp涓熺敤鏉ョ殑錛屽彲浠ヨ鐢ㄥ湪浠諱綍闇瑕佸嚱鏁扮殑鍦版柟銆?/font> </p> <p> <font face="Arial" size="2">鍑轟簬鍘嗗彶鐨勫師鍥狅紝<tt>lambda</tt> 鍑芥暟鐨勮娉曚笌閫氬父鐨勫嚱鏁版湁浜涚粏寰殑涓嶅悓銆?/font> </p> <p> <a name="c40ab5b9b4"> </a> <b> <font face="Arial" size="2">渚?2.20. <tt>lambda</tt> 鍑芥暟浠嬬粛<br /></font> </b> <font face="Arial"> <font size="2"> <tt>>>> </tt> <span id="hvzpftn" class="userinput"> <tt class="keyword">def</tt> <tt class="function">f</tt>(x):</span>聽聽聽聽聽聽聽聽聽 </font> </font> <a name="apihelper.lambda.1.1"> </a> <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> <br /> <tt>...聽聽聽聽 </tt> <span id="hvzpftn" class="userinput"> <tt class="keyword">return</tt> x*2</span> <br /> <tt>...聽聽聽聽 </tt> <span id="hvzpftn" class="userinput"> </span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">f(3)</span> <br /> <span id="hvzpftn" class="computeroutput">6</span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">g = <tt class="keyword">lambda</tt> x: x*2</span>聽 </font> <a name="apihelper.lambda.1.2"> </a> <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput">g(3)</span> <br /> <span id="hvzpftn" class="computeroutput">6</span> <br /> <tt>>>> </tt> <span id="hvzpftn" class="userinput"> <tt class="keyword">(lambda</tt> x: x*2)(3)</span> </font> <a name="apihelper.lambda.1.3"> </a> <font face="Arial"> <font size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> <br /> <span id="hvzpftn" class="computeroutput">6</span> </font> </font> </p> <table summary="Callout list" border="0"> <tbody> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">榪欐槸涓涓氬父鐨勫嚱鏁板0鏄庯紝灝界浠ュ墠浣犲彲鑳芥病鏈夌湅鍒拌繃瀹氫箟鍦ㄤ氦浜掑紡紿楀彛涓殑鍑芥暟銆傝繖涓?... 璇存槑瀹冩槸涓涓琛岀殑浜や簰璇彞銆傚彧瑕佸湪絎竴琛岀殑鍚庨潰鏁插叆鍥炶濺錛孭ython IDE浼氳浣犳帴鐫杈撳叆鍛戒護銆?/font> </p> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">榪欐槸涓涓?<tt>lambda</tt> 鍑芥暟錛屽畠瀹屾垚鍚屼笂闈㈡櫘閫氬嚱鏁扮浉鍚岀殑浜嬫儏銆傛敞鎰忚繖閲岀殑綆鐭殑璇硶錛涙病鏈夊皬鎷彿錛?<tt>return</tt> 鏄粯璁ょ殑錛屽茍涓斿嚱鏁版病鏈夊悕瀛楋紝鍙湁灝嗗畠璧嬪肩粰鍙橀噺鐨勫彉閲忓悕銆?/font> </p> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">浣犵敋鑷沖彲浠ヤ笉灝?<tt>lambda</tt> 鍑芥暟璧嬪肩粰涓涓彉閲忚屼嬌鐢ㄥ畠銆傝繖涓嶆槸涓句笘鏃犲弻鐨勪笢瑗匡紝瀹冨彧鏄睍紺轟簡 lambda 鍑芥暟鍙槸涓涓唴鑱斿嚱鏁般?/font> </p> </td> </tr> </tbody> </table> <p> <font face="Arial" size="2">鎬諱箣錛?<tt>lambda</tt> 鍑芥暟鏄竴涓彲浠ユ帴鏀朵換鎰忓涓弬鏁?鍖呮嫭</font> <a title="2.3. Optional and named arguments" > <font face="Arial" size="2">鍙夊弬鏁?/font> </a> <font face="Arial" size="2">)騫朵笖榪斿洖鍗曚釜琛ㄨ揪寮忓肩殑鍑芥暟銆?<tt>lambda</tt> 鍑芥暟涓嶈兘鍖呭惈鍛戒護錛屽畠浠墍鍖呭惈鐨勮〃杈懼紡涓嶈兘瓚呰繃涓涓備笉瑕佽瘯鍥懼悜 <tt>lambda</tt> 鍑芥暟涓鍏ュお澶氱殑涓滆タ錛涘鏋滀綘闇瑕佹洿澶嶆潅鐨勪笢瑗匡紝搴旇瀹氫箟涓涓櫘閫氬嚱鏁幫紝鐒跺悗鎯寵瀹冨闀垮氨澶氶暱銆?/font> </p> <p> <a name="tip.lambda"> <font face="Arial" size="2"> </font> </a> </p> <table class="note" border="0"> <tbody> <tr> <td valign="top" align="middle" width="25" rowspan="2"> <font face="Arial" size="2"> <img height="25" alt="Note" src="http://cn.diveintopython.org/images/note.jpg" width="25" /> </font> </td> </tr> <tr> <td valign="top" align="left" colspan="2"> <font face="Arial"> <font size="2"> <tt>lambda</tt> 鍑芥暟鏄鏍奸棶棰樸備笉涓瀹氶潪瑕佷嬌鐢ㄥ畠浠紝浠諱綍鑳藉浣跨敤瀹冧滑鐨勫湴鏂癸紝閮藉彲浠ュ畾涔変竴涓垎紱葷殑鏅氱殑鍑芥暟錛岀敤瀹冩潵鏇挎崲銆傛垜灝嗗畠浠敤鍦ㄩ渶瑕佸皝瑁呯壒孌婄殑錛岄潪閲嶇敤鐨勪唬鐮佷笂錛岀敤璁稿灝忕殑涓琛屽嚱鏁頒笉浼氬紕涔辨垜鐨勪唬鐮併?/font> </font> </td> </tr> </tbody> </table> <p> <a name="c40ab5b9b7"> </a> <b> <font face="Arial" size="2">渚?2.21. 鍦?in <tt>apihelper.py</tt> 涓殑 <tt>lambda</tt> 鍑芥暟</font>聽<br /></b> <font face="Arial" size="2">processFunc = collapse <tt class="keyword">and</tt> (<tt class="keyword">lambda</tt> s: <tt class="string">" "</tt>.join(s.split())) <tt class="keyword">or</tt> (<tt class="keyword">lambda</tt> s: s)</font> <br /> </p> <p> <font face="Arial" size="2">欏轟究榪欓噷鏈夊嚑浠朵簨鎯呴渶瑕佹敞鎰忋傞鍏堬紝鎴戜滑浣跨敤浜?</font> <a title="2.7. The and-or trick" > <tt> <font face="Arial" size="2">and-or</font> </tt> </a> <font face="Arial" size="2">鎶宸х殑綆鍗曞艦寮忥紝娌¢棶棰橈紝鍥犱負涓涓?<tt>lambda</tt> 鍑芥暟</font> <a title="Note: What's true in Python?" > <font face="Arial" size="2">鍦ㄤ竴涓竷灝旂幆澧冧笅</font> </a> <font face="Arial" size="2">鎬諱負鐪熴?榪欏茍涓嶆剰鍛崇潃 <tt>lambda</tt> 鍑芥暟涓嶈兘榪斿洖鍋囧箋傚嚱鏁版湰韜繪槸涓虹湡錛屽畠鐨勮繑鍥炲煎彲浠ヤ負浠諱綍鍊箋?</font> </p> <p> <font face="Arial" size="2">絎簩錛屾垜浠嬌鐢ㄤ簡 <tt>split</tt> 鍑芥暟娌″甫鍙傛暟銆備綘宸茬粡鐪嬪埌榪囧畠甯?/font> <a title="Example 1.34. Splitting a string" > <font face="Arial" size="2">1涓垨2涓弬鏁?/font> </a> <font face="Arial" size="2">鐨勪嬌鐢紝浣嗘槸涓嶅甫鍙傛暟瀹冩寜絀虹櫧榪涜鍒嗗壊銆?/font> </p> <p> <a name="c40ab5b9c10"> </a> <b> <font face="Arial" size="2">渚?2.22. <tt>split</tt> 涓嶅甫鍙傛暟</font>聽</b>聽聽聽<br /><font face="Arial"><font size="2"><tt>>>></tt><span id="hvzpftn" class="userinput">s = <tt class="string">"this聽聽 is\na\ttest"</tt></span>聽聽聽聽</font></font><font face="Arial" size="2">聽聽聽聽 <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /><br /><tt>>>></tt><span id="hvzpftn" class="userinput"><tt class="keyword">print</tt> s</span></font>聽聽<br /><font face="Arial"><font size="2">聽<span id="hvzpftn" class="computeroutput">this聽聽 is<br />a test</span><br /><tt>>>></tt><span id="hvzpftn" class="userinput"><tt class="keyword">print</tt> s.split()</span></font></font><font face="Arial" size="2">聽聽聽聽聽 <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /><br /><span id="hvzpftn" class="computeroutput">['this', 'is', 'a', 'test']</span><br /><tt>>>></tt><span id="hvzpftn" class="userinput"><tt class="keyword">print</tt>聽聽聽<tt class="string">" "</tt>.join(s.split())</span></font><font face="Arial"><font size="2">聽聽聽聽聽聽聽 <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /><br /><span id="hvzpftn" class="computeroutput">'this is a test'</span>聽聽聽聽</font></font></p> <table summary="Callout list" border="0"> <tbody> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="1" src="http://cn.diveintopython.org/images/callouts/1.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">榪欐槸涓涓琛屽瓧絎︿覆錛岄氳繃杞箟瀛楃鐨勫畾涔変唬鏇夸簡</font> <a title="Example 1.4. Defining the buildConnectionString function's doc string" > <font face="Arial" size="2">涓夐噸寮曞彿</font> </a> <font face="Arial" size="2">銆?<tt>\n</tt> 鏄竴涓洖杞︼紱 <tt>\t</tt> 鏄竴涓埗琛ㄧ銆?/font> </p> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="2" src="http://cn.diveintopython.org/images/callouts/2.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial"> <font size="2"> <tt>split</tt> 涓嶅甫鍙傛暟鎸夌┖鐧借繘琛屽垎鍓層傛墍浠ヤ笁涓┖鏍鹼紝涓涓洖杞︼紝鍜屼竴涓埗琛ㄧ閮芥槸涓鏍風殑銆?/font> </font> </p> </td> </tr> <tr> <td valign="top" align="left" width="12"> <a > <font face="Arial" size="2"> <img height="12" alt="3" src="http://cn.diveintopython.org/images/callouts/3.jpg" width="12" border="0" /> </font> </a> <font face="Arial" size="2"> </font> </td> <td valign="top" align="left"> <p> <font face="Arial" size="2">浣犲彲浠ュ皢絀虹櫧緇熶竴鍖栵紝閫氳繃鍒嗗壊涓涓瓧絎︿覆錛岀劧鍚庣敤鍗曚釜絀烘牸浣滀負鍒嗛殧絎﹀皢鍏墮噸鏂版帴璧鋒潵銆傝繖灝辨槸 <tt>help</tt> 鍑芥暟鎵鍋氱殑錛屽皢澶氳鏂囨。瀛楃涓插悎騫舵垚鍗曡銆?/font> </p> </td> </tr> </tbody> </table> <p> <font face="Arial" size="2">閭d箞 <tt>help</tt> 鍑芥暟鍒板簳鐢ㄨ繖浜?<tt>lambda</tt> 鍑芥暟錛?<tt>split</tt> 鍑芥暟錛屽拰 <tt>and-or</tt> 鎶宸у仛浜嗕粈涔堝憿錛?</font> </p> <p> <a name="apihelper.funcassign"> </a> <b> <font face="Arial" size="2">渚?2.23. 灝嗗嚱鏁拌祴緇欎竴涓彉閲?/font> </b> </p> <pre> <font face="Arial" size="2"> processFunc = collapse <tt class="keyword">and</tt> (<tt class="keyword">lambda</tt> s: <tt class="string">" "</tt>.join(s.split())) <tt class="keyword">or</tt> (<tt class="keyword">lambda</tt> s: s)</font> </pre> <p> <font face="Arial"> <font size="2"> <tt>processFunc</tt> 鐜板湪鏄竴涓嚱鏁幫紝浣嗗畠涓哄摢涓涓嚱鏁拌鐪?<tt>collapse</tt> 鍙橀噺鐨勫箋傚鏋?<tt>collapse</tt> 涓虹湡錛?<tt><tt>processFunc</tt>(<i>string</i>)</tt> 灝嗗帇緙╃┖鐧斤紱鍚﹀垯錛?tt><tt>processFunc</tt>(<i>string</i>)</tt> 灝嗚繑鍥炴湭鏀瑰彉鐨勫弬鏁般?</font> </font> </p> <p> <font face="Arial" size="2">鍦ㄤ竴涓笉寰堝緩澹殑璇█瀹炵幇瀹冿紝璞B錛屼綘灝嗗彲鑳藉垱寤轟竴涓嚱鏁幫紝瀹冩帴鏀朵竴涓瓧絎︿覆鍜屼竴涓?<i><tt>collapse</tt></i> 鍙傛暟錛屼嬌鐢ㄤ竴涓?<tt>if</tt> 璇彞鏉ュ垽鏂槸鍚﹁鍘嬬緝絀虹櫧鎴栦笉鍘嬬緝錛岀劧鍚庤繑鍥炵浉搴旂殑鍊箋傝繖鏍鋒晥鐜囦綆錛屽洜涓哄嚱鏁板皢涓嶅緱涓嶅鐞嗘瘡縐嶅彲鑳芥э紱姣忔浣犺皟鐢ㄥ畠錛屽畠灝嗕笉寰椾笉鍦ㄧ粰鍑轟綘鎵鎯寵鐨勪笢瑗夸箣鍓嶏紝鍒ゆ柇鏄惁瑕佸帇緙╃┖鐧姐傚湪Python涓紝浣犲彲浠ュ皢閭g鍒ゆ柇閫昏緫鎷垮埌鍑芥暟澶栭潰錛岃屽畾涔変竴涓鍑忚繃鐨?<tt>lambda</tt> 鍑芥暟鏉ョ粰鍑虹‘鍒囩殑(騫朵笖鍞竴)浣犳兂瑕佺殑銆傝繖鏍峰仛鏇存湁鏁堢巼錛屾洿婕備寒錛屽茍涓旀洿灝戝鑷撮偅浜涗護浜鴻鍘岀殑(鍝︼紝鎯沖埌閭d簺鍙傛暟灝卞ご鏄?鐨勯敊璇?br /></font> </p> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5494.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-13 21:49 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5494.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python 瀛︿範涔嬭礬錛圔rief Outlook of Library:Two錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5484.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Thu, 13 Apr 2006 11:45:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5484.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5484.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5484.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5484.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5484.html</trackback:ping><description><![CDATA[ <a name="CHILD_LINKS"> <font face="Tahoma"> <font size="2"> <strong>Subsections</strong> </font> </font> </a> <ul class="ChildLinks"> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.1 Output Formatting</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.2 Templating</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.3 Working with Binary Data Record Layouts</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.4 Multi-threading</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.5 Logging</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.6 Weak References</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.7 Tools for Working with Lists</font> </font> </font> </a> </li> <li> <a > <font face="Tahoma"> <font size="2"> <font color="#002c99">11.8 Decimal Floating Point Arithmetic</font> </font> </font> </a> </li> </ul> <!--End of Table of Child-Links--> <font face="Tahoma" size="2"> <hr /> </font> <h1> <a name="SECTION0013000000000000000000"> </a> <a name="briefTourTwo"> </a> <br /> <font face="Tahoma" size="2">11. Brief Tour of the Standard Library - Part II </font> </h1> <p> <font face="Tahoma" size="2">This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts. </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013100000000000000000"> </a> <a name="output-formatting"> </a> <br /> <font face="Tahoma" size="2">11.1 Output Formatting </font> </h1> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">repr</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides an version of <tt class="function">repr()</tt> for abbreviated displays of large or deeply nested containers: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import repr >>> repr.repr(set('supercalifragilisticexpialidocious')) "set(['a', 'c', 'd', 'e', 'f', 'g', ...])" </font> </pre> </div> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">pprint</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module offers more sophisticated control over printing both built-in and user defined objects in a way that is readable by the interpreter. When the result is longer than one line, the ``pretty printer'' adds line breaks and indentation to more clearly reveal data structure: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import pprint >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', ... 'yellow'], 'blue']]] ... >>> pprint.pprint(t, width=30) [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]] </font> </pre> </div> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">textwrap</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module formats paragraphs of text to fit a given screen width: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import textwrap >>> doc = """The wrap() method is just like fill() except that it returns ... a list of strings instead of one big string with newlines to separate ... the wrapped lines.""" ... >>> print textwrap.fill(doc, width=40) The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines. </font> </pre> </div> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">locale</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module accesses a database of culture specific data formats. The grouping attribute of locale's format function provides a direct way of formatting numbers with group separators: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import locale >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252') 'English_United States.1252' >>> conv = locale.localeconv() # get a mapping of conventions >>> x = 1234567.8 >>> locale.format("%d", x, grouping=True) '1,234,567' >>> locale.format("%s%.*f", (conv['currency_symbol'], ... conv['frac_digits'], x), grouping=True) '$1,234,567.80' </font> </pre> </div> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013200000000000000000"> </a> <a name="templating"> </a> <br /> <font face="Tahoma" size="2">11.2 Templating </font> </h1> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">string</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module includes a versatile <tt class="class">Template</tt> class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application. </font> </p> <p> <font face="Tahoma" size="2">The format uses placeholder names formed by "<tt class="samp">$</tt>" with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing "<tt class="samp">$$</tt>" creates a single escaped "<tt class="samp">$</tt>": </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> from string import Template >>> t = Template('${village}folk send $$10 to $cause.') >>> t.substitute(village='Nottingham', cause='the ditch fund') 'Nottinghamfolk send $10 to the ditch fund.' </font> </pre> </div> <p> <font face="Tahoma" size="2">The <tt class="method">substitute</tt> method raises a <tt class="exception">KeyError</tt> when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the <tt class="method">safe_substitute</tt> method may be more appropriate -- it will leave placeholders unchanged if data is missing: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> t = Template('Return the $item to $owner.') >>> d = dict(item='unladen swallow') >>> t.substitute(d) Traceback (most recent call last): . . . KeyError: 'owner' >>> t.safe_substitute(d) 'Return the unladen swallow to $owner.' </font> </pre> </div> <p> <font face="Tahoma" size="2">Template subclasses can specify a custom delimiter. For example, a batch renaming utility for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or file format: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import time, os.path >>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg'] >>> class BatchRename(Template): ... delimiter = '%' >>> fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format): ') Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f >>> t = BatchRename(fmt) >>> date = time.strftime('%d%b%y') >>> for i, filename in enumerate(photofiles): ... base, ext = os.path.splitext(filename) ... newname = t.substitute(d=date, n=i, f=ext) ... print '%s --> %s' % (filename, newname) img_1074.jpg --> Ashley_0.jpg img_1076.jpg --> Ashley_1.jpg img_1077.jpg --> Ashley_2.jpg </font> </pre> </div> <p> <font face="Tahoma" size="2">Another application for templating is separating program logic from the details of multiple output formats. This makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports. </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013300000000000000000"> </a> <a name="binary-formats"> </a> <br /> <font face="Tahoma" size="2">11.3 Working with Binary Data Record Layouts </font> </h1> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">struct</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides <tt class="function">pack()</tt> and <tt class="function">unpack()</tt> functions for working with variable length binary record formats. The following example shows how to loop through header information in a ZIP file (with pack codes <code>"H"</code> and <code>"L"</code> representing two and four byte unsigned numbers respectively): </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> import struct data = open('myfile.zip', 'rb').read() start = 0 for i in range(3): # show the first 3 file headers start += 14 fields = struct.unpack('LLLHH', data[start:start+16]) crc32, comp_size, uncomp_size, filenamesize, extra_size = fields start += 16 filename = data[start:start+filenamesize] start += filenamesize extra = data[start:start+extra_size] print filename, hex(crc32), comp_size, uncomp_size start += extra_size + comp_size # skip to the next header </font> </pre> </div> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013400000000000000000"> </a> <a name="multi-threading"> </a> <br /> <font face="Tahoma" size="2">11.4 Multi-threading </font> </h1> <p> <font face="Tahoma" size="2">Threading is a technique for decoupling tasks which are not sequentially dependent. Threads can be used to improve the responsiveness of applications that accept user input while other tasks run in the background. A related use case is running I/O in parallel with computations in another thread. </font> </p> <p> <font face="Tahoma" size="2">The following code shows how the high level </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">threading</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module can run tasks in background while the main program continues to run: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print 'Finished background zip of: ', self.infile background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() print 'The main program continues to run in foreground.' background.join() # Wait for the background task to finish print 'Main program waited until background was done.' </font> </pre> </div> <p> <font face="Tahoma" size="2">The principal challenge of multi-threaded applications is coordinating threads that share data or other resources. To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores. </font> </p> <p> <font face="Tahoma" size="2">While those tools are powerful, minor design errors can result in problems that are difficult to reproduce. So, the preferred approach to task coordination is to concentrate all access to a resource in a single thread and then use the </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">Queue</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module to feed that thread with requests from other threads. Applications using <tt class="class">Queue</tt> objects for inter-thread communication and coordination are easier to design, more readable, and more reliable. </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013500000000000000000"> </a> <a name="logging"> </a> <br /> <font face="Tahoma" size="2">11.5 Logging </font> </h1> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">logging</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module offers a full featured and flexible logging system. At its simplest, log messages are sent to a file or to <code>sys.stderr</code>: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> import logging logging.debug('Debugging information') logging.info('Informational message') logging.warning('Warning:config file %s not found', 'server.conf') logging.error('Error occurred') logging.critical('Critical error -- shutting down') </font> </pre> </div> <p> <font face="Tahoma" size="2">This produces the following output: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> WARNING:root:Warning:config file server.conf not found ERROR:root:Error occurred CRITICAL:root:Critical error -- shutting down </font> </pre> </div> <p> <font face="Tahoma" size="2">By default, informational and debugging messages are suppressed and the output is sent to standard error. Other output options include routing messages through email, datagrams, sockets, or to an HTTP Server. New filters can select different routing based on message priority: <tt class="constant">DEBUG</tt>, <tt class="constant">INFO</tt>, <tt class="constant">WARNING</tt>, <tt class="constant">ERROR</tt>, and <tt class="constant">CRITICAL</tt>. </font> </p> <p> <font face="Tahoma" size="2">The logging system can be configured directly from Python or can be loaded from a user editable configuration file for customized logging without altering the application. </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013600000000000000000"> </a> <a name="weak-references"> </a> <br /> <font face="Tahoma" size="2">11.6 Weak References </font> </h1> <p> <font face="Tahoma" size="2">Python does automatic memory management (reference counting for most objects and garbage collection to eliminate cycles). The memory is freed shortly after the last reference to it has been eliminated. </font> </p> <p> <font face="Tahoma" size="2">This approach works fine for most applications but occasionally there is a need to track objects only as long as they are being used by something else. Unfortunately, just tracking them creates a reference that makes them permanent. The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">weakref</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides tools for tracking objects without creating a reference. When the object is no longer needed, it is automatically removed from a weakref table and a callback is triggered for weakref objects. Typical applications include caching objects that are expensive to create: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import weakref, gc >>> class A: ... def __init__(self, value): ... self.value = value ... def __repr__(self): ... return str(self.value) ... >>> a = A(10) # create a reference >>> d = weakref.WeakValueDictionary() >>> d['primary'] = a # does not create a reference >>> d['primary'] # fetch the object if it is still alive 10 >>> del a # remove the one reference >>> gc.collect() # run garbage collection right away 0 >>> d['primary'] # entry was automatically removed Traceback (most recent call last): File "<pyshell#108>", line 1, in -toplevel- d['primary'] # entry was automatically removed File "C:/PY24/lib/weakref.py", line 46, in __getitem__ o = self.data[key]() KeyError: 'primary' </font> </pre> </div> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013700000000000000000"> </a> <a name="list-tools"> </a> <br /> <font face="Tahoma" size="2">11.7 Tools for Working with Lists </font> </h1> <p> <font face="Tahoma" size="2">Many data structure needs can be met with the built-in list type. However, sometimes there is a need for alternative implementations with different performance trade-offs. </font> </p> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">array</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides an <tt class="class">array()</tt> object that is like a list that stores only homogenous data but stores it more compactly. The following example shows an array of numbers stored as two byte unsigned binary numbers (typecode <code>"H"</code>) rather than the usual 16 bytes per entry for regular lists of python int objects: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> from array import array >>> a = array('H', [4000, 10, 700, 22222]) >>> sum(a) 26932 >>> a[1:3] array('H', [10, 700]) </font> </pre> </div> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">collections</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides a <tt class="class">deque()</tt> object that is like a list with faster appends and pops from the left side but slower lookups in the middle. These objects are well suited for implementing queues and breadth first tree searches: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> from collections import deque >>> d = deque(["task1", "task2", "task3"]) >>> d.append("task4") >>> print "Handling", d.popleft() Handling task1 unsearched = deque([starting_node]) def breadth_first_search(unsearched): node = unsearched.popleft() for m in gen_moves(node): if is_goal(m): return m unsearched.append(m) </font> </pre> </div> <p> <font face="Tahoma" size="2">In addition to alternative list implementations, the library also offers other tools such as the </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">bisect</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module with functions for manipulating sorted lists: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> import bisect >>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')] >>> bisect.insort(scores, (300, 'ruby')) >>> scores [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')] </font> </pre> </div> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">heapq</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module provides functions for implementing heaps based on regular lists. The lowest valued entry is always kept at position zero. This is useful for applications which repeatedly access the smallest element but do not want to run a full list sort: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> from heapq import heapify, heappop, heappush >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] >>> heapify(data) # rearrange the list into heap order >>> heappush(data, -5) # add a new entry >>> [heappop(data) for i in range(3)] # fetch the three smallest entries [-5, 0, 1] </font> </pre> </div> <p> <font face="Tahoma" size="2"> </font> </p> <h1> <a name="SECTION0013800000000000000000"> </a> <a name="decimal-fp"> </a> <br /> <font face="Tahoma" size="2">11.8 Decimal Floating Point Arithmetic </font> </h1> <p> <font face="Tahoma" size="2">The </font> <a class="ulink" > <tt class="module"> <font face="Tahoma"> <font size="2"> <font color="#002c99">decimal</font> </font> </font> </tt> </a> <font face="Tahoma" size="2">module offers a <tt class="class">Decimal</tt> datatype for decimal floating point arithmetic. Compared to the built-in <tt class="class">float</tt> implementation of binary floating point, the new class is especially helpful for financial applications and other uses which require exact decimal representation, control over precision, control over rounding to meet legal or regulatory requirements, tracking of significant decimal places, or for applications where the user expects the results to match calculations done by hand. </font> </p> <p> <font face="Tahoma" size="2">For example, calculating a 5% tax on a 70 cent phone charge gives different results in decimal floating point and binary floating point. The difference becomes significant if the results are rounded to the nearest cent: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> from decimal import * >>> Decimal('0.70') * Decimal('1.05') Decimal("0.7350") >>> .70 * 1.05 0.73499999999999999 </font> </pre> </div> <p> <font face="Tahoma" size="2">The <tt class="class">Decimal</tt> result keeps a trailing zero, automatically inferring four place significance from multiplicands with two place significance. Decimal reproduces mathematics as done by hand and avoids issues that can arise when binary floating point cannot exactly represent decimal quantities. </font> </p> <p> <font face="Tahoma" size="2">Exact representation enables the <tt class="class">Decimal</tt> class to perform modulo calculations and equality tests that are unsuitable for binary floating point: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> Decimal('1.00') % Decimal('.10') Decimal("0.00") >>> 1.00 % 0.10 0.09999999999999995 >>> sum([Decimal('0.1')]*10) == Decimal('1.0') True >>> sum([0.1]*10) == 1.0 False </font> </pre> </div> <p> <font face="Tahoma" size="2">The <tt class="module">decimal</tt> module provides arithmetic with as much precision as needed: </font> </p> <p> <font face="Tahoma" size="2"> </font> </p> <div id="hvzpftn" class="verbatim"> <pre> <font face="Tahoma" size="2"> >>> getcontext().prec = 36 >>> Decimal(1) / Decimal(7) Decimal("0.142857142857142857142857142857142857") </font> </pre> </div> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5484.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-13 19:45 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5484.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python 瀛︿範涔嬭礬錛圔rief Outlook of Library:One錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5483.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Thu, 13 Apr 2006 11:43:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5483.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5483.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5483.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5483.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5483.html</trackback:ping><description><![CDATA[ <a name="CHILD_LINKS"> <strong>Subsections</strong> </a> <ul class="ChildLinks"> <li> <a >10.1 Operating System Interface</a> </li> <li> <a >10.2 File Wildcards</a> </li> <li> <a >10.3 Command Line Arguments</a> </li> <li> <a >10.4 Error Output Redirection and Program Termination</a> </li> <li> <a >10.5 String Pattern Matching</a> </li> <li> <a >10.6 Mathematics</a> </li> <li> <a >10.7 Internet Access</a> </li> <li> <a >10.8 Dates and Times</a> </li> <li> <a >10.9 Data Compression</a> </li> <li> <a >10.10 Performance Measurement</a> </li> <li> <a >10.11 Quality Control</a> </li> <li> <a >10.12 Batteries Included</a> </li> </ul> <!--End of Table of Child-Links--> <hr /> <h1> <a name="SECTION0012000000000000000000"> </a> <a name="briefTour"> </a> <br />10. Brief Tour of the Standard Library </h1> <p> </p> <h1> <a name="SECTION0012100000000000000000"> </a> <a name="os-interface"> </a> <br />10.1 Operating System Interface </h1> <p>The <a class="ulink" ><tt class="module">os</tt></a> module provides dozens of functions for interacting with the operating system: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import os >>> os.system('time 0:02') 0 >>> os.getcwd() # Return the current working directory 'C:\\Python24' >>> os.chdir('/server/accesslogs') </pre> </div> <p>Be sure to use the "<tt class="samp">import os</tt>" style instead of "<tt class="samp">from os import *</tt>". This will keep <tt class="function">os.open()</tt> from shadowing the builtin <tt class="function">open()</tt> function which operates much differently. </p> <p> <a id="l2h-34" xml:id="l2h-34"> </a>The builtin <tt class="function">dir()</tt> and <tt class="function">help()</tt> functions are useful as interactive aids for working with large modules like <tt class="module">os</tt>: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import os >>> dir(os) <returns a list of all module functions> >>> help(os) <returns an extensive manual page created from the module's docstrings> </pre> </div> <p>For daily file and directory management tasks, the <a class="ulink" ><tt class="module">shutil</tt></a> module provides a higher level interface that is easier to use: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import shutil >>> shutil.copyfile('data.db', 'archive.db') >>> shutil.move('/build/executables', 'installdir') </pre> </div> <p> </p> <h1> <a name="SECTION0012200000000000000000"> </a> <a name="file-wildcards"> </a> <br />10.2 File Wildcards </h1> <p>The <a class="ulink" ><tt class="module">glob</tt></a> module provides a function for making file lists from directory wildcard searches: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import glob >>> glob.glob('*.py') ['primes.py', 'random.py', 'quote.py'] </pre> </div> <p> </p> <h1> <a name="SECTION0012300000000000000000"> </a> <a name="command-line-arguments"> </a> <br />10.3 Command Line Arguments </h1> <p>Common utility scripts often need to process command line arguments. These arguments are stored in the <a class="ulink" ><tt class="module">sys</tt></a> module's <var>argv</var> attribute as a list. For instance the following output results from running "<tt class="samp">python demo.py one two three</tt>" at the command line: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import sys >>> print sys.argv ['demo.py', 'one', 'two', 'three'] </pre> </div> <p>The <a class="ulink" ><tt class="module">getopt</tt></a> module processes <var>sys.argv</var> using the conventions of the <span id="hvzpftn" class="Unix">Unix</span><tt class="function">getopt()</tt> function. More powerful and flexible command line processing is provided by the <a class="ulink" ><tt class="module">optparse</tt></a> module. </p> <p> </p> <h1> <a name="SECTION0012400000000000000000"> </a> <a name="stderr"> </a> <br />10.4 Error Output Redirection and Program Termination </h1> <p>The <a class="ulink" ><tt class="module">sys</tt></a> module also has attributes for <var>stdin</var>, <var>stdout</var>, and <var>stderr</var>. The latter is useful for emitting warnings and error messages to make them visible even when <var>stdout</var> has been redirected: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> sys.stderr.write('Warning, log file not found starting a new one\n') Warning, log file not found starting a new one </pre> </div> <p>The most direct way to terminate a script is to use "<tt class="samp">sys.exit()</tt>". </p> <p> </p> <h1> <a name="SECTION0012500000000000000000"> </a> <a name="string-pattern-matching"> </a> <br />10.5 String Pattern Matching </h1> <p>The <a class="ulink" ><tt class="module">re</tt></a> module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import re >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') ['foot', 'fell', 'fastest'] >>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') 'cat in the hat' </pre> </div> <p>When only simple capabilities are needed, string methods are preferred because they are easier to read and debug: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> 'tea for too'.replace('too', 'two') 'tea for two' </pre> </div> <p> </p> <h1> <a name="SECTION0012600000000000000000"> </a> <a name="mathematics"> </a> <br />10.6 Mathematics </h1> <p>The <a class="ulink" ><tt class="module">math</tt></a> module gives access to the underlying C library functions for floating point math: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import math >>> math.cos(math.pi / 4.0) 0.70710678118654757 >>> math.log(1024, 2) 10.0 </pre> </div> <p>The <a class="ulink" ><tt class="module">random</tt></a> module provides tools for making random selections: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import random >>> random.choice(['apple', 'pear', 'banana']) 'apple' >>> random.sample(xrange(100), 10) # sampling without replacement [30, 83, 16, 4, 8, 81, 41, 50, 18, 33] >>> random.random() # random float 0.17970987693706186 >>> random.randrange(6) # random integer chosen from range(6) 4 </pre> </div> <p> </p> <h1> <a name="SECTION0012700000000000000000"> </a> <a name="internet-access"> </a> <br />10.7 Internet Access </h1> <p>There are a number of modules for accessing the internet and processing internet protocols. Two of the simplest are <a class="ulink" ><tt class="module">urllib2</tt></a> for retrieving data from urls and <a class="ulink" ><tt class="module">smtplib</tt></a> for sending mail: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import urllib2 >>> for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'): ... if 'EST' in line: # look for Eastern Standard Time ... print line <BR>Nov. 25, 09:43:32 PM EST >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org From: soothsayer@example.org Beware the Ides of March. """) >>> server.quit() </pre> </div> <p> </p> <h1> <a name="SECTION0012800000000000000000"> </a> <a name="dates-and-times"> </a> <br />10.8 Dates and Times </h1> <p>The <a class="ulink" ><tt class="module">datetime</tt></a> module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient member extraction for output formatting and manipulation. The module also supports objects that are time zone aware. </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre># dates are easily constructed and formatted >>> from datetime import date >>> now = date.today() >>> now datetime.date(2003, 12, 2) >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.' # dates support calendar arithmetic >>> birthday = date(1964, 7, 31) >>> age = now - birthday >>> age.days 14368 </pre> </div> <p> </p> <h1> <a name="SECTION0012900000000000000000"> </a> <a name="data-compression"> </a> <br />10.9 Data Compression </h1> <p>Common data archiving and compression formats are directly supported by modules including: <a class="ulink" ><tt class="module">zlib</tt></a>, <a class="ulink" ><tt class="module">gzip</tt></a>, <a class="ulink" ><tt class="module">bz2</tt></a>, <a class="ulink" ><tt class="module">zipfile</tt></a>, and <a class="ulink" ><tt class="module">tarfile</tt></a>. </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> import zlib >>> s = 'witch which has which witches wrist watch' >>> len(s) 41 >>> t = zlib.compress(s) >>> len(t) 37 >>> zlib.decompress(t) 'witch which has which witches wrist watch' >>> zlib.crc32(s) 226805979 </pre> </div> <p> </p> <h1> <a name="SECTION00121000000000000000000"> </a> <a name="performance-measurement"> </a> <br />10.10 Performance Measurement </h1> <p>Some Python users develop a deep interest in knowing the relative performance of different approaches to the same problem. Python provides a measurement tool that answers those questions immediately. </p> <p>For example, it may be tempting to use the tuple packing and unpacking feature instead of the traditional approach to swapping arguments. The <a class="ulink" ><tt class="module">timeit</tt></a> module quickly demonstrates a modest performance advantage: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>>>> from timeit import Timer >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit() 0.57535828626024577 >>> Timer('a,b = b,a', 'a=1; b=2').timeit() 0.54962537085770791 </pre> </div> <p>In contrast to <tt class="module">timeit</tt>'s fine level of granularity, the <a class="ulink" ><tt class="module">profile</tt></a> and <tt class="module">pstats</tt> modules provide tools for identifying time critical sections in larger blocks of code. </p> <p> </p> <h1> <a name="SECTION00121100000000000000000"> </a> <a name="quality-control"> </a> <br />10.11 Quality Control </h1> <p>One approach for developing high quality software is to write tests for each function as it is developed and to run those tests frequently during the development process. </p> <p>The <a class="ulink" ><tt class="module">doctest</tt></a> module provides a tool for scanning a module and validating tests embedded in a program's docstrings. Test construction is as simple as cutting-and-pasting a typical call along with its results into the docstring. This improves the documentation by providing the user with an example and it allows the doctest module to make sure the code remains true to the documentation: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>def average(values): """Computes the arithmetic mean of a list of numbers. >>> print average([20, 30, 70]) 40.0 """ return sum(values, 0.0) / len(values) import doctest doctest.testmod() # automatically validate the embedded tests </pre> </div> <p>The <a class="ulink" ><tt class="module">unittest</tt></a> module is not as effortless as the <tt class="module">doctest</tt> module, but it allows a more comprehensive set of tests to be maintained in a separate file: </p> <p> </p> <div id="hvzpftn" class="verbatim"> <pre>import unittest class TestStatisticalFunctions(unittest.TestCase): def test_average(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3) self.assertRaises(ZeroDivisionError, average, []) self.assertRaises(TypeError, average, 20, 30, 70) unittest.main() # Calling from the command line invokes all tests </pre> </div> <p> </p> <h1> <a name="SECTION00121200000000000000000"> </a> <a name="batteries-included"> </a> <br />10.12 Batteries Included </h1> <p>Python has a ``batteries included'' philosophy. This is best seen through the sophisticated and robust capabilities of its larger packages. For example: </p> <p> </p> <ul> <li>The <a class="ulink" ><tt class="module">xmlrpclib</tt></a> and <a class="ulink" ><tt class="module">SimpleXMLRPCServer</tt></a> modules make implementing remote procedure calls into an almost trivial task. Despite the names, no direct knowledge or handling of XML is needed. </li> <li>The <a class="ulink" ><tt class="module">email</tt></a> package is a library for managing email messages, including MIME and other RFC 2822-based message documents. Unlike <tt class="module">smtplib</tt> and <tt class="module">poplib</tt> which actually send and receive messages, the email package has a complete toolset for building or decoding complex message structures (including attachments) and for implementing internet encoding and header protocols. </li> <li>The <a class="ulink" ><tt class="module">xml.dom</tt></a> and <a class="ulink" ><tt class="module">xml.sax</tt></a> packages provide robust support for parsing this popular data interchange format. Likewise, the <a class="ulink" ><tt class="module">csv</tt></a> module supports direct reads and writes in a common database format. Together, these modules and packages greatly simplify data interchange between python applications and other tools. </li> <li>Internationalization is supported by a number of modules including <a class="ulink" ><tt class="module">gettext</tt></a>, <a class="ulink" ><tt class="module">locale</tt></a>, and the <a class="ulink" ><tt class="module">codecs</tt></a> package. </li> </ul> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-13 19:43 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5483.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python瀛︿範涔嬭礬錛堜笁錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5444.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Thu, 13 Apr 2006 03:26:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5444.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5444.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5444.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5444.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5444.html</trackback:ping><description><![CDATA[ <p> <font style="BACKGROUND-COLOR: #f2f2f2"> <font face="Arial"> <font size="2"> <strong>Python Reading Notes : (2006-4-13)<br />Note One : the powerful Lists</strong> <br /> </font> </font> <font color="#0000ff"> <font size="2"> <font face="Arial"> <em>Using Lists as Stacks<br /><font color="#000000">聽</font><br /></em> </font> <font face="Tahoma" color="#000000">To add an item to the top of the stack, use <em>append()</em> ,To retrieve an item from the top of the stack, use <em>pop()</em> without an explicit index. For example: <br /></font> </font> <font face="Tahoma" color="#000000" size="2">>>> stack = [3, 4, 5]<br />>>> stack.append(6)<br />>>> stack.append(7)<br />>>> stack<br />[3, 4, 5, 6, 7]<br />>>> stack.pop()<br />7<br />>>> stack<br />[3, 4, 5, 6]<br />>>> stack.pop()<br />6<br />>>> stack.pop()<br />5<br />>>> stack<br />[3, 4]<br /><br /></font> <em> <font face="Arial" size="2">Using Lists as Queues</font> </em> </font> <font face="Arial"> <font size="2"> <br /> <br /> <font face="Tahoma">To add an item to the back of the queue, use <em>append() </em>To retrieve an item from the front of the queue, use <em>pop()</em> with <em>0 </em>as the index. For example: <br /></font> </font> <font face="Tahoma" size="2">>>> queue = ["Eric", "John", "Michael"]<br />>>> queue.append("Terry")聽聽聽聽聽聽聽聽聽聽 # Terry arrives<br />>>> queue.append("Graham")聽聽聽聽聽聽聽聽聽 # Graham arrives<br />>>> queue.pop(0)<br />'Eric'<br />>>> queue.pop(0)<br />'John'<br />>>> queue<br />['Michael', 'Terry', 'Graham']<br /></font> </font> </font> </p> <p> <font size="2"> <font style="BACKGROUND-COLOR: #f2f2f2"> <font face="Arial"> <strong>Note聽Two :about Tuples (distinguish between string type and聽 tuples tpye,especially聽zero or only one items contained in a tuples聽)<br /><br /></strong> <font face="Tahoma">A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example: <br /></font> <font face="Tahoma">>>> empty = ()<br />>>> singleton = 'hello',聽聽聽 # <-- note trailing comma<br />>>> len(empty)<br />0<br />>>> len(singleton)<br />1<br />>>> singleton<br />('hello',)<br /><br /><br />but if you write a statement like this : <br />>>> singleton = 'hello'聽聽聽聽聽聽聽聽聽 # it means that you define or construct a string type,not a tuples<br />>>>聽singleton<br />'hello'<br />聽 </font> <br /> <br /> </font> </font> </font> </p> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5444.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-13 11:26 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5444.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python瀛︿範涔嬭礬錛堜簩錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5441.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Thu, 13 Apr 2006 02:50:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5441.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5441.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5441.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5441.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5441.html</trackback:ping><description><![CDATA[ <strong> <font face="Arial" size="2">Python Reading Notes (2006-4-13)</font> </strong>聽<br /><br /><font face="Arial"><font size="2"><strong>Note One錛歛bout list (wow! so powerful)</strong><br /><br />Assignment to slices is also possible, and this can even change the size of the list,but you can not do that in the string type: <br />>>> # Replace some items:<br />... a[0:2] = [1, 12]<br />>>> a<br />[1, 12, 123, 1234]<br />>>> # Remove some:<br />... a[0:2] = []<br />>>> a<br />[123, 1234]<br />>>> # Insert some:<br />... a[1:1] = ['bletch', 'xyzzy']<br />>>> a<br />[123, 'bletch', 'xyzzy', 1234]<br />>>> a[:0] = a聽聽聽聽 # Insert (a copy of) itself at the beginning<br />>>> a<br />[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]<br /><br /></font><p><font face="Tahoma" size="2">It is possible to nest lists (create lists containing other lists), for example: <br /></font><font size="2"><font face="Tahoma">>>> q = [2, 3]<br />>>> p = [1, q, 4]<br />>>> len(p)<br />3<br />>>> p[1]<br />[2, 3]<br />>>> p[1][0]<br />2<br />>>> p[1].append('xtra')聽聽聽聽<br />>>> p<br />[1, [2, 3, 'xtra'], 4]<br />>>> q<br />[2, 3, 'xtra']</font><br /><br /><strong>Note Two : about for statement</strong></font></p><p><font size="2">It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient: <br /><br /><em>valid statements: it will work normally<br /></em>>>> for x in a[:]: # make a slice copy of the entire list<br />...聽聽聽聽聽聽 if len(x) > 6:聽<br />...聽聽聽聽聽聽聽聽聽聽聽a.insert(0, x)<br />... <br />>>> a<br />['defenestrate', 'cat', 'window', 'defenestrate']<br /></font></p><p><font size="2"><em>invalid statement: you prompt will die</em><br /></font><font size="2">>>> for x in a :聽 # make a slice copy of the entire list<br />...聽聽聽 聽聽聽if len(x) > 6:<br />...聽聽聽聽聽聽聽聽聽 a.insert(0, x)<br />... </font></p><p><font size="2"><u>Note that:</u> in the first statement聽the first row ,the聽for statement聽ues 'a[:] ' ,it means that聽to make a slice of聽it;but in the second statement,the for statement ues 'a' directly ,so cause a聽bad聽result聽</font><br /><br /><strong><font size="2">Note Three: Compare for efficiency<br /></font></strong><br /><font size="2">str_test = []<br />#聽method one :<br />str_test.append('attach')<br />#method two:<br />str_test = str_test聽+ ['attach']<br /><br />The聽Two聽method above has the聽same function ,but the more efficient one is the first one<br />聽<br /></font><font size="2"><strong>Note Four :about Defining Functions聽<br /></strong><u>Form 1:Default Argument Values</u><br />The default value is evaluated only once.<br /></font><font face="Arial" size="2">def f(a, L=[]):<br />聽聽聽 L.append(a)<br />聽聽聽 return L<br /><br />print f(1)<br />print f(2)<br />print f(3)</font></p><p><font size="2">This will print </font></p><p><font size="2"></font></p><div id="hvzpftn" class="verbatim"><pre><font face="Arial" size="2">[1] [1, 2] [1, 2, 3] </font></pre></div><p><font size="2">If you don't want the default to be shared between subsequent calls, you can write the function like this instead: </font></p><p><font size="2"></font></p><div id="hvzpftn" class="verbatim"><pre><font face="Arial" size="2">def f(a, L=None): if L is None: L = [] L.append(a) return L<br /><br /><u>Form 2 : Keyword Arguments</u><br /></font><p><font face="Arial" size="2">When a final formal parameter of the form <em>**name</em> is present, it receives a </font><a class="ulink" ><font face="Arial" color="#002c99" size="2">dictionary</font></a><font face="Arial" size="2"> containing all keyword arguments except<br /> for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in <br />the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. <em>(*name</em> must<br /> occur before <em>**name</em>) For example, if we define a function like this: </font></p><p><font face="Arial" size="2"></font></p><div id="hvzpftn" class="verbatim"><pre><font face="Arial" size="2"><em>def cheeseshop(kind, *arguments, **keywords): print "-- Do you have any", kind, '?' print "-- I'm sorry, we're all out of", kind for arg in arguments: print arg print '-'*40<br /> keys = keywords.keys() keys.sort() for kw in keys: print kw, ':', keywords[kw]</em></font></pre></div><p><font face="Arial" size="2">It could be called like this: </font></p><p><font face="Arial" size="2"></font></p><div id="hvzpftn" class="verbatim"><pre><font face="Arial" size="2"><em>cheeseshop('Limburger', "It's very runny, sir.", "It's really very, VERY runny, sir.", client='John Cleese', shopkeeper='Michael Palin', sketch='Cheese Shop Sketch')</em></font></pre></div><p><font face="Arial" size="2">and of course it would print: </font></p><p><font face="Arial" size="2"></font></p><div id="hvzpftn" class="verbatim"><pre><font face="Arial" size="2"><em>-- Do you have any Limburger ? -- I'm sorry, we're all out of Limburger It's very runny, sir. It's really very, VERY runny, sir. ---------------------------------------- client : John Cleese shopkeeper : Michael Palin sketch : Cheese Shop Sketch</em></font></pre></div><p><font face="Arial" size="2">Note that the <em>sort()</em> method of the list of keyword argument names is called before printing the contents of the <em> keywords<br /></em>dictionary; if this is not done, the order in which the arguments are printed is undefined. </font></p><font size="3"></font></pre></div></font><img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5441.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-13 10:50 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/13/5441.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python 瀛︿範涔嬭礬錛堜竴錛?/title><link>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5413.html</link><dc:creator>Jeff-Chen</dc:creator><author>Jeff-Chen</author><pubDate>Wed, 12 Apr 2006 11:00:00 GMT</pubDate><guid>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5413.html</guid><wfw:comment>http://m.shnenglu.com/Jeff-Chen/comments/5413.html</wfw:comment><comments>http://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5413.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5413.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Jeff-Chen/services/trackbacks/5413.html</trackback:ping><description><![CDATA[ <strong> <font face="Arial" size="2">Python璇諱功絎旇( 2006-4-12 )錛?/font> <br /> <br /> </strong> <font face="Tahoma"> <font size="2"> <strong>Note One聽聽聽: about integer and long integer</strong> <br />There are four types of numeric literals: plain integers, long integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). <br /></font> </font> <font face="Tahoma"> <font size="2">Note that numeric literals do not include a sign; a phrase like <code>-1</code> is actually an expression composed of the unary operator `<code>-</code>' and the literal 1<br /><br /></font> <strong> <font size="2">Note Two聽聽聽: about operators<br /></font> </strong> </font> <font face="Tahoma"> <font size="2">The comparison operators <code><></code> and <code>!=</code> are alternate spellings of the same operator. <code>!=</code> is the preferred spelling; <code><></code> is obsolescent. <br /><br /></font> <font size="2"> <strong>Note Three: about delimiters<br /></strong>The following printing ASCII characters are not used in Python. Their occurrence outside string literals and comments is an unconditional error:聽 聽$聽聽聽聽聽聽 ?<br /><br /></font> <font size="2"> <strong>Note Four聽 : about <strong>Sequences</strong><br /></strong>Sequences also support slicing: <font face="Arial">a[i:j] </font>selects all items with index <var>k</var> such that聽 i <= k < j 聽When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support ``extended slicing'' with a third ``step'' parameter:聽<em><font face="Courier New">a[i:j:k]</font></em> selects all items of <var>a</var> with index <var>x</var> where聽 x = i +n*k聽, n >= 0聽<var></var>聽and i <= x < j.</font> </font> <font size="2"> <br /> <br /> </font> <font size="2"> <font face="Arial"> <strong>Note Five聽聽聽: about Primary Prompt<br /></strong> </font> <font face="Tahoma">In interactive mode, the last printed expression is assigned to the variable <font face="Tahoma"><code>_</code>. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example: <br /></font><font face="Tahoma"><br />>>> tax = 12.5 / 100<br />>>> price = 100.50<br />>>> price * tax<br />12.5625<br />>>> price + _<br />113.0625<br />>>> round(_, 2)<br />113.06<br />>>><br /></font><br />This variable should be treated as read-only by the user. Don't explicitly assign a value to it -- you would create an independent local variable with the same name masking the built-in variable with its magic behavior. </font> </font> <img src ="http://m.shnenglu.com/Jeff-Chen/aggbug/5413.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Jeff-Chen/" target="_blank">Jeff-Chen</a> 2006-04-12 19:00 <a href="http://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5413.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍒濊瘑 python!http://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5411.htmlJeff-ChenJeff-ChenWed, 12 Apr 2006 10:14:00 GMThttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5411.htmlhttp://m.shnenglu.com/Jeff-Chen/comments/5411.htmlhttp://m.shnenglu.com/Jeff-Chen/archive/2006/04/12/5411.html#Feedback2http://m.shnenglu.com/Jeff-Chen/comments/commentRss/5411.htmlhttp://m.shnenglu.com/Jeff-Chen/services/trackbacks/5411.html 涓巔ython絎竴嬈′翰瀵嗘帴瑙?
鍦ㄧ綉涓婃悳闆嗕簡寰堝璧勬枡鍜屾祻瑙堜簡python鐨勫緢澶氱浉鍏蟲枃绔狅紝瀹冪粰鎴戠殑鎬諱綋鎰熻鏄湪鏂囨湰澶勭悊涓婄殑楂樻晥鍜岃璦鐨?浜烘у寲",
闈婦涓緥瀛愭潵璇存槑鎴戜負浠涔堣寰?浜烘у寲":

1.聽Reason聽One :聽闀挎暣鍨嬫棤闀垮害闄愬埗錛岄櫎浜嗙數鑴戠‖浠剁殑闄愬埗
Although both lower case "l" and upper case "L" are allowed as suffix for long integers, it is strongly recommended to always use "L", since the letter "l" looks too much like the digit "1".
Plain integer literals that are above the largest representable plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted as if they were long integers instead.
There is no limit for long integer literals apart from what can be stored in available memory. Some examples of plain integer literals (first row) and long integer literals (second and third rows):聽聽聽聽聽聽聽
7聽聽聽聽聽 2147483647聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 0177
3L聽聽聽 79228162514264337593543950336L聽聽聽 0377L聽聽 0x100000000L
聽聽聽聽聽聽 79228162514264337593543950336聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 0xdeadbeef


2.聽Reason聽Two : (quote) 涓涓猵ython灝弔ip錛屼氦鎹袱涓彉閲忕殑鍊鹼紝鑰屼笖涓嶄嬌鐢ㄧ涓変釜鍙橀噺錛?
>>> a,b = 1,2
>>> a,b = b,a
>>> a,b
(2, 1)
>>>聽

3.Reason Three:
瀹冩槸鐢–璇█寮鍙戠殑錛屼絾鏄晥鐜囨瘮璧稢璇█涔熷樊浜嗗灝戯紝褰撶劧娌蹇暒錛屼絾鏄粬閲岄潰鐨勫緢澶氬嚱鏁伴潪甯告柟渚匡紝鍔熻兘姣擟寮哄ぇ寰堝銆?br />4.Reason Four :銆
鏈璁╀漢鍏村鐨勫師鍥犲氨鏄疊itComet灝辨槸瀹冨紑鍙戝嚭鏉ョ殑銆?br />
浠栭伩鍏嶄簡寰堝鍒濆紼嬪簭璁捐鏃跺父甯稿嚭鐜扮殑闂錛屽敖綆″浜庡ぇ澶氭暟浜烘潵璇撮兘鏄嚑涔庝笉浼氬彂鐢熺殑錛屼絾鏄粠榪欑偣鍙互鐪嬪嚭瀹冨瀹冪殑鐢ㄦ埛鏄偦鐡滃紑濮嬬殑銆傚洜涓哄偦鐡滄墠浼氱姱鍝簺閿欒錛?br />




Jeff-Chen 2006-04-12 18:14 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              欧美日韩精品中文字幕| 欧美高清视频在线播放| 国产亚洲综合在线| 国产欧美不卡| 国产夜色精品一区二区av| 国产精品一区二区三区久久久| 国产精品成人免费视频 | 欧美特黄a级高清免费大片a级| 欧美99久久| 欧美日韩国产精品成人| 欧美日韩系列| 国产无遮挡一区二区三区毛片日本| 国产一区二区在线观看免费| 樱桃国产成人精品视频| 99精品视频一区| 亚洲免费在线观看| 久久久久国色av免费看影院| 亚洲国产乱码最新视频| 在线视频免费在线观看一区二区| 99精品视频免费观看| 新67194成人永久网站| 看片网站欧美日韩| 欧美体内she精视频| 黑人一区二区三区四区五区| 亚洲伦理在线观看| 欧美一区二区精美| 亚洲黄色一区| 欧美一区二区三区的| 欧美激情一区| 伊人久久亚洲热| 亚洲欧美制服另类日韩| 欧美成人性网| 欧美一级理论片| 欧美伦理视频网站| 伊人夜夜躁av伊人久久| 国产精品99久久久久久久久 | 性做久久久久久免费观看欧美| 老司机一区二区三区| 99re6这里只有精品视频在线观看| 亚洲一区二区毛片| 久久综合亚州| 亚洲欧美国产视频| 国产精品成人va在线观看| 最近中文字幕mv在线一区二区三区四区 | 欧美伊人久久| 正在播放亚洲一区| 欧美护士18xxxxhd| 亚洲第一页中文字幕| 久久xxxx精品视频| 中文日韩电影网站| 欧美国产日本在线| 亚洲国产日韩在线| 久热精品视频在线| 亚洲欧美另类在线| 国产精品免费区二区三区观看| 亚洲美女在线看| 欧美激情第五页| 久久一区二区三区四区| 韩国精品主播一区二区在线观看| 午夜精品在线| 亚洲自拍偷拍麻豆| 国产午夜精品美女毛片视频| 亚洲欧美日韩国产一区二区| 在线亚洲国产精品网站| 国产精品成人一区二区三区吃奶| 亚洲一区二区欧美| 一本色道久久精品| 国产精品久久久久99| 亚洲伊人网站| 午夜在线电影亚洲一区| 国产亚洲欧美日韩日本| 久久国产精彩视频| 欧美一区二区三区男人的天堂 | 亚洲动漫精品| 欧美二区在线看| aⅴ色国产欧美| 亚洲视频在线观看网站| 国产欧美精品日韩区二区麻豆天美| 欧美伊人久久久久久午夜久久久久| 午夜视频一区二区| 亚洲电影第三页| 亚洲激情成人在线| 国产精品久久久一区二区三区 | 亚洲精品久久久久久久久久久久| 欧美日韩国产天堂| 午夜在线一区| 久久久蜜臀国产一区二区| 亚洲精品男同| 亚洲一区视频在线| 136国产福利精品导航| 亚洲人成网站777色婷婷| 国产精品视频久久| 免费欧美日韩国产三级电影| 欧美色视频一区| 激情视频一区| 99re在线精品| 在线激情影院一区| 夜夜嗨av色综合久久久综合网| 国产精品色在线| 亚洲国产经典视频| 国产日韩精品视频一区二区三区| 欧美国产免费| 国产精品视频网站| 亚洲国产合集| 精品999久久久| 亚洲自拍电影| 亚洲美女啪啪| 久久久夜精品| 久久国产精品久久久| 美女黄毛**国产精品啪啪| 亚洲欧美一区二区三区极速播放| 另类亚洲自拍| 久久精品成人一区二区三区| 国内自拍视频一区二区三区| 亚洲激情另类| 亚洲三级视频在线观看| 黄色成人91| 欧美顶级艳妇交换群宴| 国产精品毛片在线看| 欧美成在线观看| 国产视频在线观看一区二区| 亚洲精品久久久久中文字幕欢迎你| 国产欧美日韩精品一区| 亚洲精品久久久蜜桃| 极品尤物av久久免费看 | 亚洲性线免费观看视频成熟| 日韩一级黄色大片| 久久亚洲一区二区| 欧美自拍偷拍| 国产精品久久久久天堂| 亚洲精品你懂的| 精品二区视频| 欧美在线视频日韩| 久久精品盗摄| 国产香蕉97碰碰久久人人| 国产精品99久久久久久有的能看| 99视频有精品| 欧美多人爱爱视频网站| 亚洲国产精品成人一区二区| 亚洲国语精品自产拍在线观看| 久久久一区二区三区| 国产精品色在线| 亚洲欧美www| 欧美资源在线| 国内外成人免费激情在线视频网站| 亚洲一区二区三区四区视频| 亚洲欧美综合国产精品一区| 国产精品免费小视频| 亚洲在线免费视频| 久久精品中文字幕免费mv| 国内精品久久久久久久果冻传媒 | 欧美一区观看| 一区二区三区在线视频观看| 久久精品亚洲国产奇米99| 久久婷婷蜜乳一本欲蜜臀| 在线播放日韩专区| 欧美成人资源网| 亚洲精品男同| 亚洲欧美制服中文字幕| 国内精品一区二区三区| 久久一区免费| 日韩视频精品在线| 性视频1819p久久| 亚洲福利视频二区| 欧美精品久久久久久| 亚洲一二区在线| 欧美3dxxxxhd| 亚洲综合视频1区| 国产自产精品| 欧美日韩精品三区| 欧美一区二区精品| 你懂的视频一区二区| 亚洲网站在线观看| 有坂深雪在线一区| 欧美色图五月天| 久久综合给合久久狠狠色 | 久久成人国产| 99国产精品久久久久久久成人热 | 国产精品99久久久久久久女警| 国产麻豆91精品| 欧美成人影音| 久久激情综合| 国产精品99久久99久久久二8| 老司机午夜免费精品视频| 亚洲午夜激情在线| 最新成人av在线| 国产一区日韩一区| 国产精品久久一区二区三区| 欧美成人一区二免费视频软件| 在线亚洲高清视频| 亚洲丶国产丶欧美一区二区三区| 欧美一级片久久久久久久| 亚洲精品欧美精品| 精品999久久久| 国产婷婷色一区二区三区四区| 欧美福利一区| 免费欧美电影| 久久久av毛片精品| 午夜免费日韩视频| 午夜激情综合网|