锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 聽 榪欐槸涓涓啀綆鍗曚笉榪囩殑璧嬪艱鍙ワ紝鍗充究鏄墠寮濮嬪涔犵紪紼嬬殑鏂版墜涔熻兘鑴卞彛鑰屽嚭瀹冪殑鍚箟 -- 鈥滆緗彉閲廼鐨勫間負1鈥濄?/p>
i = 2
聽 鈥滃皢鍙橀噺i鐨勫兼敼涓?鈥濓紝褰撶湅鍒版帴涓嬫潵榪欒浠g爜鏃訛紝浣犺剳嫻蜂腑鑲畾浼氱珛鍗蟲誕鐜拌繖鏍風殑蹇靛ご銆?/p>
聽 榪欓毦閬撲細鏈夐棶棰樺槢錛熻繖綆綆鍗曞崟鐨勪竴琛岃祴鍊艱鍙ュ叾瀹炲寘鍚簡python涓殑涓変釜閲嶈姒傚康錛氬悕瀛椼佺粦瀹氬拰瀵硅薄銆?br />python瀵硅祴鍊艱鍙ヤ綔鍑轟簡鑷繁鐨勫畾涔夛細 聽 鍚嶅瓧緇戝畾鍒板璞¤繖涓蹇靛湪python涓殢澶勫彲瑙侊紝鍙互璇存槸python鐨勬渶鍩烘湰鑰屼笖鏈閲嶈鐨勬蹇典箣涓銆傚鏋?br />娌℃湁寰堝ソ鐞嗚В榪欎竴鐐癸紝涓浜涙剰鎯充笉鍒扮殑緇撴灉灝變細鍦ㄦ偍鐨勪唬鐮佷腑鎮勭劧鍑虹幇銆?/p>
聽 鍏堟潵鐪嬩竴涓畝鍗曚緥瀛愶細
>>> a = {'g':1} 聽 鍑轟箮鎰忔枡鍢涳紵璇鋒參鎱㈢湅瀹岃繖綃囨枃绔犮?/p>
1. 瀵硅薄 聽 鍦╬ython涓紝鎵鏈夊璞¢兘鏈変笅闈笁涓壒寰侊細 聽 涓鏃﹀璞¤鍒涘緩錛屽畠鐨勬爣璇嗙爜灝變笉鍏佽鏇存敼銆傚璞$殑鏍囪瘑鐮佸彲浠ユ湁鍐呭緩鍑芥暟id()鑾峰彇錛屽畠鏄竴涓暣鍨嬫暟銆傛偍鍙互灝嗗畠鎯寵薄涓鴻瀵硅薄鍦ㄥ唴瀛樹腑鐨勫湴鍧錛屽叾瀹炲湪鐩墠鐨勫疄鐜頒腑鏍囪瘑鐮佷篃灝辨槸璇ュ璞$殑鍐呭瓨鍦板潃銆?/p>
>>> class c1: 聽 鎹㈢畻涓涓嬶紝11274040灝辨槸鍗佸叚榪涘埗鐨?x00AC0738銆?/p>
>>> id(1)
聽 榪欏氨鏄墠闈㈡彁鍒扮殑1榪欎釜瀵硅薄鐨勬爣璇嗙爜錛屼篃灝辨槸瀹冨湪鍐呭瓨涓殑鍦板潃銆?/font>
聽 褰撶敤is鎿嶄綔絎︽瘮杈冧袱涓璞℃椂錛屽氨鏄湪姣旇緝瀹冧滑鐨勬爣璇嗙爜銆傛洿紜垏鍦拌錛宨s鎿嶄綔絎︽槸鍦ㄥ垽鏂袱涓璞℃槸鍚︽槸鍚屼竴涓璞°?br />>>> [1] is [1]
>>> [1] == [1]
聽 涓庡璞$殑鏍囪瘑鐮佺被浼鹼紝瀵硅薄鐨勭被鍨嬩篃鏄笉鍙洿鏀圭殑銆傚彲浠ョ敤鍐呭緩鍑芥暟type()鍙栧緱瀵硅薄鐨勭被鍨嬨?/p>
聽 鏈夌殑瀵硅薄鐨勫兼槸鍙互鏀瑰彉鐨勶紝榪欑被瀵硅薄鍙綔鍙彉瀵硅薄錛涜屽彟澶栦竴浜涘璞″湪鍒涘緩鍚庡叾鍊兼槸涓嶅彲鏀瑰彉鐨勶紙濡?榪欎釜瀵硅薄錛夛紝榪欑被瀵硅薄鍙綔鎭掑畾瀵硅薄銆傚璞$殑鍙彉鎬ф槸鐢卞畠鐨勭被鍨嬪喅瀹氱殑錛屾瘮濡傛暟鍊煎瀷(number)銆佸瓧絎︿覆鍨?string)浠ュ強搴忓垪鍨?tuple)鐨勫璞℃槸鎭掑畾瀵硅薄錛涜屽瓧鍏稿瀷(dictionary)鍜屽垪琛ㄥ瀷(list)鐨勫璞℃槸鍙彉瀵硅薄銆?/p>
聽 闄や簡涓婇潰鎻愬埌鐨勪笁涓壒寰佸錛屼竴涓璞″彲鑳斤細 2. 鍚嶅瓧 聽 鍚嶅瓧鍦ㄤ竴瀹氱殑鍚嶅瓧絀洪棿鍐呮湁鏁堬紝鑰屼笖鍞竴錛屼笉鍙兘鍦ㄥ悓涓涓悕瀛楃┖闂村唴鏈変袱涓垨鏇村鐨勫璞″彇鍚屼竴鍚嶅瓧銆?/p>
聽 璁╂垜浠啀鏉ョ湅鐪嬫湰綃囩殑絎竴涓緥瀛愶細i = 1銆傚湪python涓紝瀹冩湁濡備笅涓や釜鍚箟錛?br />聽* 鍒涘緩涓涓間負1鐨勬暣鍨嬪璞?br />聽* "i"鏄寚鍚戣鏁村瀷瀵硅薄鐨勫悕瀛楋紙鑰屼笖瀹冩槸涓涓紩鐢級
i=i+1
* 榪欏垱寤轟簡涓涓柊鐨勫璞★紝鍏跺間負i+1銆?br />* "i"榪欎釜鍚嶅瓧鎸囧悜浜嗚鏂板緩鐨勫璞★紝璇ュ璞$殑寮曠敤璁℃暟鍔犱竴錛岃?i"浠ュ墠鎵鎸囧悜鐨勮佸璞$殑 3.1 寮曠敤璁℃暟 聽 瀵硅薄鐨勫紩鐢ㄨ鏁板湪涓嬪垪鎯呭喌涓嬩細鍑忓皯錛?br />聽* 紱誨紑浜嗗綋鍓嶇殑鍚嶅瓧絀洪棿錛堣鍚嶅瓧絀洪棿涓殑鏈湴鍚嶅瓧閮戒細琚攢姣侊級 聽 褰撳璞$殑寮曠敤璁℃暟闄嶅埌0鍚庯紝璇ュ璞″氨浼氳閿姣侊紝鍏舵墍鍗犵殑鍐呭瓨涔熷氨寰椾互鍥炴敹銆?/p>
4. 鍚嶅瓧緇戝畾鎵甯︽潵鐨勪竴浜涘鐗圭幇璞?/p>
渚?.1錛?br />>>> li1 = [7, 8, 9, 10] 娉ㄨВ錛氳繖閲宭i1涓巐i2閮芥寚鍚戝悓涓涓垪琛ㄥ璞7, 8, 9, 10]錛屸渓i[1] = 16鈥濇槸鏀瑰彉璇ュ垪琛ㄤ腑鐨勭2涓厓绱狅紝鎵浠ラ氳繃li2鏃跺悓鏍蜂細鐪嬪埌榪欎竴鏀瑰姩銆?/p>
渚?.2錛?br />>>> b = [{'g':1}]*4
渚?.3錛?br />
>>> b = [{'g':1}] + [{'g':1}] + [{'g':1}] + [{'g':1}]
娉ㄨВ錛氬湪鏈夌殑python涔︿腑璁插埌涔樻硶絎﹀彿錛?錛夊氨鐩稿綋浜庡嚑涓姞娉曠殑閲嶅錛屽嵆璁や負渚?.2搴旇涓?.3鐨勭粨鏋滀竴鑷淬?br />聽聽聽聽聽 鍏跺疄涓嶇劧銆備緥4.2涓殑b榪欎釜鍒楄〃涓殑姣忎竴涓厓绱爗'g': 1}鍏跺疄閮芥槸鍚屼竴涓璞★紝鍙互鐢╥d(b[n])榪涜楠岃瘉銆傝屼緥4.3涓垯鏄洓涓笉鍚岀殑瀵硅薄銆傛垜浠彲浠ラ噰鐢ㄥ悕瀛楃粦瀹氱殑鏂規硶娑堥櫎榪欎竴姝т箟錛?/font>
>>> a = {'g' : 1}
>>> a = {'g' : 1} 聽 涓嶈繃瀵逛簬鎭掑畾瀵硅薄鑰岃█錛屸?鈥濆拰榪炵畫鍔犳硶鐨勬晥鏋滀竴鏍楓傛瘮濡傦紝b=[1] * 4 灝辯瓑鍚屼簬 b=[1]+[1]+[1]+[1]銆?/p>
5. 鍑芥暟鐨勪紶鍙傞棶棰?br />聽 鍑芥暟鐨勫弬鏁頒紶閫掍篃鏄竴涓悕瀛椾笌瀵硅薄鐨勭粦瀹氳繃紼嬶紝鑰屼笖鏄粦瀹氬埌鍙﹀涓涓悕瀛楃┖闂達紙鍗沖嚱鏁頒綋鍐呴儴鐨勫悕瀛楃┖闂達級銆俻ython瀵硅祴鍊艱鍙ョ殑鐙壒鐪嬫硶鍙堜細瀵瑰嚱鏁扮殑浼犻掗犳垚浠涔堝獎鍝嶅憿錛?/p>
5.1 浼犲鹼紵浼犲潃錛?br />聽 鍦ㄥ涔燙++鐨勬椂鍊欐垜浠兘鐭ラ亾鏈変袱縐嶅弬鏁頒紶閫掓柟寮忥細浼犲煎拰浼犲潃銆傝屽湪python涓墍鏈夌殑鍙傛暟浼犻掗兘鏄紩鐢ㄤ紶閫掞紙pass reference錛夛紝涔熷氨鏄紶鍧銆傝繖鏄敱浜庡悕瀛楁槸瀵硅薄鐨勪竴涓紩鐢ㄨ繖涓python鐨勭壒鎬ц岃嚜鐒跺緱鏉ョ殑錛屽湪鍑芥暟浣撳唴閮ㄥ鏌愪竴澶栭儴鍙彉瀵硅薄浣滀簡淇敼鑲畾浼氬皢鍏舵敼鍙樺甫鍒板嚱鏁頒互澶栥傝鎴戜滑鏉ョ湅鐪嬩笅闈?br />榪欎釜渚嬪瓙錛?/p>
渚?.1 聽 鍥犳錛屽湪python涓紝鎴戜滑搴旇鎶涘紑浼犻掑弬鏁拌繖縐嶆蹇碉紝鏃跺埢鐗㈣鍑芥暟鐨勮皟鐢ㄥ弬鏁版槸灝嗗璞$敤鍙﹀涓涓悕瀛楃┖闂寸殑鍚嶅瓧緇戝畾銆傚湪鍑芥暟涓紝涓嶈繃鏄敤浜嗗彟澶栦竴涓悕瀛楋紝浣嗚繕鏄榪欏悓涓涓璞¤繘琛屾搷浣溿?/p>
5.2 緙虹渷鍙傛暟 渚?.2 聽 鍑轟簡浠涔堥棶棰橈紵榪欎釜鍙傛暟par濂藉儚綾諱技涓嶤涓殑闈欐佸彉閲忥紝绱浜嗕互鍓嶇殑緇撴灉銆傛槸榪欐牱鍚楋紵褰撶劧涓嶆槸錛岃繖閮芥槸鈥滃璞°佸悕瀛椼佺粦瀹氣濊繖浜涙濇兂鎯圭殑鈥滅ジ鈥濄傗滀竾鐗╃殕瀵硅薄鈥濓紝榪樿寰楀悧錛熻繖閲岋紝鍑芥暟foo褰撶劧涔熸槸涓涓璞★紝鍙互縐頒箣涓哄嚱鏁板璞★紙涓庝竴鑸殑瀵硅薄娌′粈涔堜笉鍚岋級銆傚厛鏉ョ湅鐪嬭繖涓璞℃湁浜涗粈涔堝睘鎬с?/p>
>>> dir(foo) 聽 鍗曚粠鍚嶅瓧涓婄湅錛屸渇unc_defaults鈥濆緢鍙兘涓庣己鐪佸弬鏁版湁鍏籌紝鐪嬬湅瀹冪殑鍊箋?/p>
>>> foo.func_defaults聽聽聽聽聽聽聽聽聽 # 鏄劇ず榪欎釜灞炴х殑鍐呭 聽 鏋滀笉鍏剁劧錛屽氨鏄繖涓簭鍒楀璞★紙tuple)鍖呭惈浜嗘墍鏈夌殑緙虹渷鍙傛暟銆傞獙璇佷竴涓嬶細
>>> def fooM(par1, def1=1, def2=[], def3='str'):聽聽聽聽聽聽聽聽聽聽 # 瀹氫箟涓涓湁澶氫釜緙虹渷鍙傛暟鐨勫嚱鏁?br />...聽def2.append(0) 聽 鍦ㄥ嚱鏁板畾涔変腑鏈夊嚑涓己鐪佸弬鏁幫紝func_defaults涓氨浼氬寘鎷嚑涓璞★紝鏆備笖縐頒箣涓虹己鐪佸弬鏁板璞★紙濡備笂鍒椾腑鐨?錛孾]鍜?str'錛夈傝繖浜涚己鐪佸弬鏁板璞$殑鐢熷懡鍛ㄦ湡涓庡嚱鏁板璞$浉鍚岋紝浠庡嚱鏁頒嬌鐢╠ef瀹氫箟寮濮嬶紝鐩村埌鍏舵秷浜★紙濡傜敤del錛夈傛墍浠ュ嵆渚挎槸鍦ㄨ繖浜涘嚱鏁版病鏈夎璋冪敤鐨勬椂鍊欙紝浣嗗彧瑕佸畾涔変簡錛岀己鐪佸弬鏁板璞″氨浼氫竴鐩村瓨鍦ㄣ?/p>
聽 鍓嶉潰璁茶繃錛屽嚱鏁拌皟鐢ㄧ殑榪囩▼灝辨槸瀵硅薄鍦ㄥ彟澶栦竴涓悕瀛楃┖闂寸殑緇戝畾榪囩▼銆傚綋鍦ㄦ瘡嬈″嚱鏁拌皟鐢ㄦ椂錛屽鏋滄病鏈変紶閫掍換浣曞弬鏁扮粰榪欎釜緙虹渷鍙傛暟錛岄偅涔堣繖涓己鐪佸弬鏁扮殑鍚嶅瓧灝變細緇戝畾鍒板湪func_defaults涓竴涓搴旂殑緙虹渷鍙傛暟瀵硅薄涓娿?br />>>> fooM(2) 聽 灝嗗嚱鏁癴oo鏀硅繘涓涓嬶紝鍙兘浼氭洿瀹規槗甯姪鐞嗚В錛?br />>>> def foo(par=[]): 聽 涓轟簡棰勯槻姝ょ被鈥滈棶棰樷濈殑鍙戠敓錛宲ython寤鴻閲囩敤涓嬪垪鏂規硶錛?br />>>> def foo(par=[]): 聽 浣跨敤None浣滀負鍝ㄥ叺錛屼互鍒ゆ柇鏄惁鏈夊弬鏁頒紶鍏ワ紝濡傛灉娌℃湁錛屽氨鏂板垱寤轟竴涓柊鐨勫垪琛ㄥ璞★紝鑰屼笉鏄粦瀹氬埌緙虹渷 6.鎬葷粨 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 鍥犳錛屽湪python涓紝鎴戜滑搴旇鎶涘紑浼犻掑弬鏁拌繖縐嶆蹇碉紝鏃跺埢鐗㈣鍑芥暟鐨勮皟鐢ㄥ弬鏁版槸灝嗗璞$敤鍙﹀涓涓悕瀛楃┖闂寸殑鍚嶅瓧緇戝畾銆傚湪鍑芥暟涓紝涓嶈繃鏄敤浜嗗彟澶栦竴涓悕瀛楋紝浣嗚繕鏄榪欏悓涓涓璞¤繘琛屾搷浣溿? a=a+1 宸茬粡鏄彟涓涓?寮曠敤"浜? The yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.
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 next() method repeatedly until it raises an exception.
When a yield statement is executed, the state of the generator is frozen and the value of expression_list is returned to next()'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 next() is invoked, the function can proceed exactly as if the yield statement were just another external call.
The yield statement is not allowed in the try clause of a try ... finally construct. The difficulty is that there's no guarantee the generator will ever be resumed, hence no guarantee that the finally block will ever get executed.
If no expressions are present, raise re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a TypeError exception is raised indicating that this is an error (if running under IDLE, a Queue.Empty exception is raised instead). Otherwise, raise evaluates the expressions to get three objects, using 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 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 If a third object is present and not The try statement specifies exception handlers and/or cleanup code for a group of statements:
There are two forms of try statement: try...except and try...finally. These forms cannot be mixed (but they can be nested in each other).
The try...except form specifies one or more exception handlers (the except clauses). When no exception occurs in the try clause, no exception handler is executed. When an exception occurs in the try 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. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack.
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 try statement raised the exception).
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.)
Before an except clause's suite is executed, details about the exception are assigned to three variables in the sys module: The optional else clause is executed if and when control flows off the end of the try clause.7.1 Exceptions in the else clause are not handled by the preceding except clauses. The try...finally form specifies a `cleanup' handler. The try clause is executed. When no exception occurs, the finally clause is executed. When an exception occurs in the try clause, the exception is temporarily saved, the finally clause is executed, and then the saved exception is re-raised. If the finally clause raises another exception or executes a return or break statement, the saved exception is lost. A continue statement is illegal in the finally 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 finally clause. When a return, break or continue statement is executed in the try suite of a try...finally statement, the finally clause is also executed `on the way out.' A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation -- this restriction may be lifted in the future). IBM developerWorks 鎻愪緵浜嗕笁縐嶄笅杞芥柟娉曪細FTP銆丠TTP 浠ュ強鐢ㄤ簬澶ф枃浠朵笅杞界殑涓嬭澆鍔╂墜錛?Download Director錛夈傚ぇ澶氭暟鎯呭喌涓嬶紝FTP 瑕佹瘮 HTTP 鏇村彲闈狅紝騫跺彲緙╃煭涓嬭澆鏃墮棿銆傛渶鏂扮殑嫻忚鍣ㄥ湪榪涜 FTP 涓嬭澆鏃朵細鏄劇ず鏃墮棿浼拌鍜屼紶杈撻熷害銆傚湪鏈変簺鎯呭喌涓嬶紝嫻忚鍣ㄨ繕鍙互緇紶涓柇鐨?FTP 涓嬭澆銆傝鍒╃敤 FTP 鐨勮繖縐嶆柟渚挎э紝鍦ㄤ嬌鐢?HTTP 涓嬭澆涔嬪墠棣栧厛灝濊瘯涓涓?FTP 涓嬭澆鏂瑰紡銆?/p>
濡傛灉鎮ㄤ嬌鐢?FTP 絀胯繃闃茬伀澧欎笅杞芥椂閬囧埌浜嗛棶棰橈紝鍐嶈瘯鐢?HTTP 涓嬭澆銆傝櫧鐒朵嬌鐢?HTTP 鏂瑰紡榪涜澶ф枃浠朵笅杞芥椂涓嶅 FTP 鍙潬錛屼絾 Internet Explore 鐢?Eclipse IDE 鍜?Apache Ant 鏋勫緩宸ュ叿榪涜 Python 寮鍙?/p>
綰у埆: 鍒濈駭
Ron Smith
, 璐熻矗浜?br /> 2004 騫?6 鏈?01 鏃?/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 璧勬簮銆? 鏈枃涓嶄細娑夊強 Eclipse銆丄nt銆丳ython 鐨勭粏鑺傘傛湁鍏寵繖浜涜瘽棰樼殑娣卞叆璁ㄨ錛岃鍙傞槄 鍙傝冭祫鏂?/font> 涓鑺備腑鐨勯摼鎺ャ? 鏈枃鐢ㄥ埌鐨勮蔣浠墮兘鍦?CPython 2.3 涓嬫祴璇曡繃銆傞櫎浜嗗嚑涓紓甯告儏鍐典箣澶栵紝搴旇涔熻兘鍦?Jython 涓繍琛屻傜壒鍒渶瑕佹寚鍑猴紝PyDev 璋冭瘯鍣ㄧ洰鍓嶄笉鏀寔 Jython銆傚彟涓涓尯鍒槸閫氳繃 Jython 鎵ц鐨勮剼鏈湪浠?PyDev 涓繍琛屼箣鍚庡氨杞叆浜や簰妯″紡錛岃繖鏍峰氨蹇呴』鎵嬪姩鏉姝匯侾yDev 緙栬緫鍣ㄤ笌 Jython 鐨勬簮浠g爜鍏煎錛孭ython Ant 浠誨姟闄?py-doc 浠誨姟涔嬪涔熷拰 Jython 鍏煎銆?
浣跨敤 Eclipse 榪涜 Python 寮鍙?/font>
Eclipse 鏄竴涓?Java 鎶鏈泦鎴愬紑鍙戠幆澧冿紝鐢?IBM 寮鍙戯紝騫跺紑鏀懼叾婧愪唬鐮併傚畠鏄?IBM 鍟嗕笟杞歡 WebSphere Application Development 鐜浠ュ強鍏朵粬澶氱宸ュ叿鐨勫熀紜銆侲clipse 鐨勫紑鍙戠ぞ鍖洪潪甯告椿璺冿紝浠栦滑涓嶄粎寮鍙?Eclipse 鏈韓錛岃繕寮鍙戝ぇ閲忕殑鎻掍歡渚?Eclipse 浣跨敤銆傛湁鍏?Eclispe 鍜?Eclipse 鎻掍歡鐨?Web 绔欑偣錛岃鍙傞槄 鍙傝冭祫鏂?/font> 涓鑺備腑鐨勯摼鎺ャ傚敖綆′粠浼犵粺涓婅 Eclipse 鏄竴縐?Java 寮鍙戝伐鍏鳳紝浣嗘槸涓浜涙彃浠剁殑瀛樺湪浣垮緱鍦?Eclipse 涓紑鍙戝叾浠栬璦鐨勭▼搴忔垚涓哄彲鑳斤紝濡?C/C++銆丳ython 鍜?Perl銆? 鍦?Eclipse 涓紝婧愪唬鐮佽緇勭粐鍒伴」鐩紙project錛変腑銆傞」鐩彲浠ュ姞杞姐佸嵏杞藉拰瀵煎叆銆侲clipse 鐢ㄦ埛鐣岄潰鐨勭粨鏋勫垝鍒嗕負瑙嗗浘錛圴iew錛変笌緙栬緫鍣紙Editor錛夈傝鍥句笌緙栬緫鍣ㄧ殑渚嬪瓙鍖呮嫭錛氭簮浠g爜澶х翰瑙嗗浘銆丣ava 婧愪唬鐮佺紪杈戝櫒銆丳ython 婧愪唬鐮佺紪杈戝櫒鍜屾枃浠剁郴緇熷鑸鍥俱侲clipse 鐢ㄦ埛鐣岄潰涓渶鍏抽敭鐨勯殣鍚蹇靛氨鏄?瑙嗚錛坧erspective錛?/i>銆傝瑙掓槸閫氬父鍦ㄦ墽琛屾煇縐嶇被鍨嬫椿鍔ㄦ椂涓璧蜂嬌鐢ㄧ殑涓緇勮鍥俱侲clipse 涓殑鏍囧噯瑙嗚鍖呮嫭錛欴ebug銆丣ava Browsing銆丣ava銆丣ava Type Hierarchy銆丳lug-in Development銆丆VS Repository Exploring銆丷esource 鍜?Install/Update銆傜洰鍓嶈繕涓嶅瓨鍦ㄥ崟鐙殑 Python 瑙嗚銆傚湪榪涜 Python 寮鍙戞椂錛屾垜閫氬父浣跨敤 Resource 瑙嗚鍜?Debug 瑙嗚銆? 棣栧厛錛屼粠 Eclipse Web 绔欑偣涓婁笅杞?Eclipse錛堣鍙傞槄 鍙傝冭祫鏂?/font> 涓鑺備腑鐨勯摼鎺ワ級錛屽茍鏍規嵁鎮ㄧ殑騫沖彴錛屾寜鐓т笅闈㈢殑瀹夎鎸囧崡瀹夎 Eclipse錛? Eclipse 鐨勬洿鏂版満鍒朵嬌 PyDev 鎻掍歡鐨勫畨瑁呮洿鍔犲鏄撱備粠 Eclipse 涓夋嫨 Help > Software Updates > Update Manager錛屽惎鍔?Install/Update 瑙嗚銆傚湪宸︿笅瑙掔殑 Feature Updates 瑙嗗浘涓紝灝?PyDev 鎻掍歡鏇存柊绔欑偣浣滀負鏂扮殑 Site Bookmark 娣誨姞鍒扳淪ites to Visit鈥濇枃浠跺す涓嬨侲clipse 鐨?PyDev 鏇存柊绔欑偣 URL 涓?http://pydev.sf.net/updates/銆傜幇鍦紝Feature Updates 緙栬緫鍣ㄤ腑搴旇鏄劇ず鍑衡淧yDev鈥濊繖涓鐗規с傚湪 Feature Updates 緙栬緫鍣ㄤ腑錛屽睍寮 PyDev > Other錛岄夋嫨鍏朵腑鏄劇ず鐨?PyDev 鐗規э紙鑷沖皯搴旇鏄?0.4.1錛夈傜劧鍚庨夋嫨 鈥淚nstall Now鈥濆畨瑁呰鐗規с侲clipse 灝嗕笅杞?PyDev 鎻掍歡錛屽茍灝嗗叾瀹夎鍒?Eclipse 涓? 涓鴻闂湰欏圭洰涓嬌鐢ㄧ殑鏍蜂緥浠g爜錛屽彲鍏堜笅杞?zip 鏂囦歡錛堣鍙傞槄 鍙傝冭祫鏂?/font>涓鑺傦級錛屽湪鏂囦歡緋葷粺涓睍寮璇?zip 鏂囦歡錛岀劧鍚庡皢鍏朵腑鐨勯」鐩鍏?Eclipse銆傚鍏ラ」鐩殑鏂規硶鏄厛鍒囨崲鍒?Resource 瑙嗚錛岄夋嫨 File > Import錛屽啀閫夋嫨鈥淓xisting Project into Workspace鈥濓紝鐒跺悗閫夋嫨鎮ㄥ睍寮 zip 鏂囦歡鐨勪綅緗傝繖鏃訛紝Navigator 瑙嗗浘涓簲璇ュ嚭鐜?feedParserTest 欏圭洰銆? 鏍蜂緥欏圭洰涓凡緇忓寘鍚簡 Fead Parser 閫氱敤璧勬簮瑙f瀽搴擄紝璇ュ簱鎸?Python 寮鏀炬簮浠g爜璁稿彲鍗忚鍙戝竷銆傛湁鍏?Feed Parser 欏圭洰 Web 緗戠珯鐨勯摼鎺ワ紝璇峰弬闃?鍙傝冭祫鏂?/font> 涓鑺傘? 鐜板湪寮濮嬪涔犲浣曢氳繃宸插鍏ョ殑欏圭洰浜嗚В PyDev 鐨勭壒鎬с侾yDev 姝e浜庡紑鍙戣繃紼嬩腑錛屼絾宸茬粡鏄潪甯擱珮鏁堢殑 Python 寮鍙戠幆澧冦傜幇鍦ㄧ殑 PyDev 涓昏鍖呮嫭浠ヤ笅鐗規э細 閫氳繃 Window > Preferences錛屽茍閫夋嫨 PyDev錛堣鍙傞槄鍥?1錛夛紝渚垮彲璁塊棶 PyDev 閫夐」銆傜涓緇勯夐」鍙互鏀瑰彉 PyDev 鍦ㄦ簮浠g爜涓鐞嗗埗琛ㄧ鐨勬柟寮忥紝榪樺彲浠ユ敼鍙樿娉曞厓绱犵殑棰滆壊銆? PyDev Debug 閫夐」鍙互閫夋嫨 Python 瑙i噴鍣紝渚涙墽琛?Python 浠g爜鏃朵嬌鐢ㄣ傚鏋?PyDev 鏃犳硶鎵懼埌 Python 瑙i噴鍣紝鎴栬呮兂浣跨敤鍒殑瑙i噴鍣紝鍙湪姝よ緗紙璇峰弬闃呭浘 2錛夈? 鎴戠殑澶ч儴鍒?Python 宸ヤ綔閮芥槸鍦?Resource 瑙嗚涓畬鎴愮殑銆備嬌鐢ㄦ柟娉曟槸鍏堝垏鎹㈠埌 Resource 瑙嗚錛岀劧鍚庡湪宸︿笂瑙掔殑 Navigator 瑙嗗浘涓弻鍑?feedParserTest/src/feedparserTest/FeedparserTest.py 鏂囦歡銆侾ython 緙栬緫鍣ㄦ墦寮璇ユ枃浠訛紝瀵?Python 璇硶榪涜瑙f瀽錛屽畬鎴愯緗鑹插拰璇硶媯鏌ョ殑宸ヤ綔錛堣鍙傞槄鍥?3錛夈? 濡傛灉婧愪唬鐮佷腑鏈変換浣曢敊璇紝鍒欐樉紺哄湪鍙充笅瑙掔殑 Tasks 瑙嗗浘涓樉紺哄嚭鏉ャ傚弻鍑?Tasks 瑙嗗浘涓殑閿欒錛屼究鍙壘鍒伴偅鏉¤鍘岀殑浠g爜琛屻? Outline 瑙嗗浘鍦ㄥ乏涓嬭錛屽叾涓敤涓縐嶄究浜庢祻瑙堢殑緇撴瀯鏄劇ず鍑哄綋鍓嶆鍦ㄧ紪杈戠殑鏂囦歡銆傚鍏ョ殑搴撱佺被銆佸嚱鏁板叏閮芥樉紺哄嚭鏉ワ紝閫氳繃鍙屽嚮 Outline 瑙嗗浘涓殑欏圭洰錛屼究鍙互瀹炵幇瀵艱埅銆侾yDev 鍦ㄧ紪杈?Python 鏂囦歡鐨勮繃紼嬩腑瀵歸綈榪涜棰勫厛瑙f瀽鐨勫伐浣滐紝鍚屾椂鏇存柊 Outline 瑙嗗浘錛屾墽琛岃娉曟鏌ワ紝騫剁敤涓嶅悓棰滆壊鏄劇ず璇硶鍏冪礌銆? PyDev 0.4 鐗堝湪 Python 婧愪唬鐮佺紪杈戝櫒涓負鍑芥暟鍜屽鍏ュ簱鍔犲叆浜嗚秴閾炬帴鐨勭壒鎬с傚鏋滃湪瓚婅繃鏌愰」瀵煎叆鎴栧嚱鏁拌皟鐢紙蹇呴』鍦? 浜轟滑宸茬粡寮濮嬪皢涓浜涗紭寮傜殑婧愪唬鐮佺紪杈戠壒鎬у姞鍏ユ渶鏂扮増鏈殑 PyDev 涓紝鍏朵腑灝卞寘鎷唬鐮佸潡娉ㄩ噴涓庡彇娑堟敞閲婏紝浠ュ強浠g爜宸﹀彸縐諱綅錛堣鍙傞槄鍥?4錛夈? 濡傛灉涓嶈兘鎵ц浠g爜錛岄偅涔?IDE 涔熶笉鏄お鏈夌敤銆備負鎵ц Python 浠g爜錛屽彲浠?Navigator 瑙嗗浘涓夋嫨 feedparser.py 鏂囦歡錛岀敤鍙抽敭鐐瑰嚮錛岀劧鍚庨夋嫨 Python > Run銆傞殢鍚庝細鏄劇ず Python 鐨勫惎鍔ㄩ厤緗獥鍙o紙璇峰弬闃呭浘 5錛夈? Python 鍚姩閰嶇疆紿楀彛涓彲浠ュ畾涔夎剼鏈墽琛岀殑褰撳墠鐩綍錛屼紶閫掔粰鑴氭湰鐨勫弬鏁幫紝浠ュ強鐢ㄥ摢涓涓?Python 瑙i噴鍣ㄨ繍琛岃剼鏈俧eedparser.py 浠ヤ竴涓?RSS URL 浣滀負鍙傛暟錛屾墍浠ュ彲鍦ㄥ弬鏁板瓧孌典腑濉叆 URL錛屽 http://www.ibm.com/developerworks/news/dw_dwtp.rss銆傚叾浣欑殑緙虹渷瀹氫箟灝卞彲浠ヤ簡錛屾墍浠ュ崟鍑?Run銆? 鑴氭湰鎵ц鏃惰緭鍑轟俊鎭樉紺哄湪 Console 紿楀彛涓傚鏋滄湁閿欒鍑虹幇錛孋onsole 紿楀彛涓皢鏄劇ず鍫嗘爤璺熻釜淇℃伅錛屽叾涓殑姣忎竴琛岄兘鍙互閫氳繃瓚呴摼鎺ユ壘鍒?Python 婧愪唬鐮併? Python 璋冭瘯鍣ㄦ槸鏈榪戞墠鍔犲叆 PyDev 鎻掍歡涓殑銆傝浣跨敤璋冭瘯鍣紝鍙湪 Python 緙栬緫鍣ㄤ腑鎯充腑鏂殑浠g爜琛岀殑宸︿晶鐐瑰嚮錛岃緗柇鐐廣傚湪鍥?6 涓紝鎴戝湪 feedparser.py 鐨?1830 琛屽璁劇疆浜嗘柇鐐廣傜劧鍚庡湪 Navigator 瑙嗗浘涓夋嫨榪欎釜 Python 妯″潡錛岀偣鍑誨彸閿紝閫夋嫨鈥淧ython > Debug...鈥濄傝繖鏃跺皢鏄劇ず涓庡墠闈㈢浉浼肩殑涓涓惎鍔ㄩ厤緗獥鍙c傜偣鍑?Debug 榪涘叆 Debug 瑙嗚錛屽悓鏃跺惎鍔ㄨ皟璇曞櫒銆? 宸︿笂瑙掔殑 Debug 瑙嗗浘鏄劇ず褰撳墠姝e湪鎵ц鐨勮繘紼嬪拰綰跨▼錛屽彸涓婅鐨?Variables 瑙嗗浘鏄劇ず褰撳墠榪愯鍩熶腑鐨勬墍鏈夊彉閲忥紝Python 緙栬緫鍣ㄤ細鏄劇ず璋冭瘯鍣ㄧ洰鍓嶅仠鍦ㄥ摢鏉¤鍙ヤ笂錛屽悓鏃舵墍鏈夌殑杈撳嚭淇℃伅閮芥樉紺轟笌 Console 瑙嗗浘涓傝皟璇曞櫒鍙互閫氳繃 Debug 瑙嗗浘搴曢儴鐨勬寜閽垨 Run 鑿滃崟榪涜鎺у埗銆? Eclipse 鍙婂叾鎻掍歡榪樺叿澶囧緢澶氬叾浠栫殑鐗規э紝鍙簲鐢ㄤ簬 Python 寮鍙戜腑錛屽 XML 緙栬緫鍣ㄣ乁ML 緙栬緫鍣紙涓嶈繃澶у鏁版槸浠?Java 浠g爜涓轟腑蹇冿級錛岃繕鏈夎祫婧愭帶鍒舵柟闈㈢殑鎻掍歡銆傜洰鍓?Eclipse 鎻掍歡绔欑偣涓婂垪鍑虹殑鎻掍歡鍑犱箮鏈?500 涓紙璇峰弬闃?鍙傝冭祫鏂?/font> 涓鑺備腑鐨勭浉鍏抽摼鎺ワ級銆傛垜灝嗙潃閲嶄粙緇嶄竴涓寰堝 Python 寮鍙戜漢鍛橀兘鐗瑰埆鏈夌敤鐨勬彃浠訛細Eclipse 鍙戣鐗堜腑宸茬粡鍖呮嫭鐨?CVS 鎻掍歡錛屼笉榪囦笉浼氳璁虹粏鑺傚唴瀹廣? Eclipse 涓寘鎷壒鎬т赴瀵岀殑闆嗘垚 CVS錛?/p>
榪樺彲浠ラ氳繃鎻愪緵鍏朵粬鎻掍歡鏉ユ敮鎸佸叾浠栨簮浠g爜鎺у埗緋葷粺錛屽 ClearCase銆丼ubversion銆乂isual SourceSafe 絳夈?
鍦?Eclipse 涓嬌鐢?Python 鐨勪氦浜掑紡 shell
Python 瑙i噴鍣ㄦ敮鎸?Python 浠g爜鐨勪氦浜掑紡鎵ц銆傝繖縐嶆柟寮忓浜庤皟璇曚竴孌典唬鐮佹槸闈炲父鏈夌敤鐨勶紝鍥犱負涓嶇敤鎶婁唬鐮佹斁榪?Python 鑴氭湰涓茍鎵ц鑴氭湰浜嗐傚悓鏃訛紝Python 瑙i噴鍣ㄧ殑浜や簰妯″紡鍙互寰堝鏄撳湴闆嗘垚鍒?Eclipse 涓? 瑕佸鍔犲 Python 浜や簰寮忔墽琛岀殑鏀寔錛屽彲閫氳繃 Run > External Tools > External Tools 澧炲姞涓涓?External Tool 鍚姩紼嬪簭銆傝繖鏃跺皢鎵撳紑 External Tool 鍚姩紼嬪簭閰嶇疆紿楀彛銆傚湪 Configurations 鍒楄〃涓夋嫨鈥淧rogram鈥濓紝鐒跺悗鐐瑰嚮鈥淣ew鈥濆垱寤轟竴涓柊鐨勯厤緗傚皢璇ラ厤緗懡鍚嶄負璇稿 "pythonInteractive" 涔嬬被錛岀劧鍚庤緗?Location錛屼護鍏舵寚鍚戞偍鐨?Python 瑙i噴鍣紝鎺ョ潃錛屽皢 "-i" 浣滀負鍞竴鐨勫弬鏁頒紶閫掕繘鏉ワ紙璇峰弬闃呭浘 7錛夈? 鍦?Common 閫夐」鍗′笅錛岄変腑澶嶉夋錛屼嬌璇ラ厤緗湪 External Tools 鏀惰棌澶硅彍鍗曚腑鏄劇ず鍑烘潵銆? 瑕佽繍琛屽垰鍒氬湪 Eclipse 涓垱寤虹殑鍚姩鍣紝鍙夋嫨 Run > External Tools > pythonInterpreter銆侾ython 瑙i噴鍣ㄧ殑杈撳嚭鏄劇ず鍦?Console 瑙嗗浘涓侰onsole 涓彲杈撳叆 Python 鍛戒護騫舵墽琛岋紝灝卞儚浠庡懡浠よ涓墽琛?Python 涓鏍楓備負瀵煎叆騫跺湪浜や簰妯″紡涓嬩嬌鐢ㄦā鍧楋紝鎮ㄩ渶瑕佸皢妯″潡鐨勪綅緗鍔犲埌 鍦?Eclipse Console 涓墽琛?Python 涓庣敤鍛戒護琛屾墽琛岀殑涓嶅悓涔嬪鍦ㄤ簬錛屾棤娉曞惎鐢ㄥ懡浠ゅ巻鍙茬壒鎬э紙閫氳繃鍚戜笂鍜屽悜涓嬬殑鏂瑰悜閿疄鐜幫級錛屽洜涓?Eclipse Console 浼氳嚜宸辮В閲婅繖浜涢敭銆? Python 浼氬湪瀹冮渶瑕佺殑鏃跺欒嚜鍔ㄧ紪璇戞ā鍧椼傝繖鎰忓懗鐫 Python 寮鍙戜漢鍛橀氬父涓嶅繀鏄懼紡鍦板妯″潡榪涜緙栬緫銆傚嵆渚垮姝わ紝鏈夋椂鍊欐墜宸ョ紪璇?Python 浠g爜榪樻槸寰堟湁鐢ㄧ殑錛屽悓鏃訛紝鏋勫緩鍜岄儴緗茶繃紼嬩腑榪樻湁寰堝鍏朵粬鏂歸潰鐨勫唴瀹瑰彲浠ヨ嚜鍔ㄥ寲瀹炵幇銆傝繖涔熸鏄瀯寤哄伐鍏風殑鐢ㄦ涔嬪湴銆? 鎴戝皢鐫閲嶄粙緇嶆潵鑷?Java 緙栫▼涓栫晫涓殑 Apache Ant錛岃繖涓伐鍏峰彲澶ч噺搴旂敤鍦?Python 寮鍙戜腑銆侫pache Ant 鏄?Java 緙栫▼棰嗗煙鍐呬簨瀹炰笂鐨勬爣鍑嗘瀯寤哄伐鍏楓傚畠鏇村姞杞諱究錛屼笌 Java 鎶鏈粨鍚堝緱鏇村ソ錛屽彲鐢ㄤ簬鏇夸唬鍏朵粬鐨勬瀯寤哄伐鍏楓侫nt 鍙互鍦ㄦ敮鎸?Java 緙栫▼璇█鐨勪換浣曚竴縐嶅鉤鍙頒笂榪愯銆傚敖綆℃垜浠渶瑕佺殑澶у鏁版瀯寤虹壒鎬?Ant 閮藉凡緇忔彁渚涗簡錛屼絾濡傛灉瑕佸皢 Ant 鐢ㄥ仛 Python 鏋勫緩宸ュ叿錛岃繕鏄渶瑕佹湁涓浜涘叧閿殑涓?Python 鐩稿叧鐨勭壒鎬с傛垜宸茬粡寮鍙戜簡鑻ュ共瀹氬埗鐨?Ant 鎻掍歡錛堢敤 Ant 鐨勮璇濊鍙仛 task錛夛紝鍙彁渚涙瀯寤?Python 鏃墮渶瑕佺殑鐗瑰畾浜?Python 鐨勭壒鎬с? Ant 鐢?XML 浣滀負鎻忚堪鏋勫緩鐨勬牸寮忋俠uild 鏂囦歡緇勭粐涓洪渶瑕佹墽琛岀殑鐩爣銆傛瘡涓涓洰鏍囬兘鍙兘渚濊禆浜庡叾浠栫殑鐩爣銆侫nt 灝嗘牴鎹偍鎵璇鋒眰鎵ц鐨勭洰鏍囷紝浠ュ強涓緇勪緷璧栫洰鏍囷紝鏉ユ墽琛屼換浣曢渶瑕佺殑鐩爣銆傛瘡涓涓洰鏍囬兘鍙兘鍖呭惈浠繪剰鏁伴噺鐨?Ant 浠誨姟錛岃岀敱 Ant 浠誨姟瀹為檯鎵ц鐩爣鐨勫伐浣溿侫nt 鏈夊緢澶氬唴緗殑浠誨姟錛屽彲浠ュ畬鎴愯濡傜紪璇?Java 浠g爜銆佺敓鎴愭枃。銆佹搷綰墊枃浠跺拰鐩綍錛屽悓鏃剁涓夋柟鍙堟彁渚涗簡寰堝闄勫姞鐨勪換鍔°? 鎴戝皢閫氳繃涓?feedparser 欏圭洰鍒涘緩鏋勫緩鑴氭湰鏉ヤ粙緇?Ant 鏋勫緩鑴氭湰鍜?Python Ant 浠誨姟鐨勫熀紜鐭ヨ瘑銆備負浜嗕嬌鐢?Python Ant 浠誨姟錛屾偍闇瑕佷笅杞藉茍瀹夎鍖呭惈榪欎簺浠誨姟鐨?Java 搴撱傞鍏堬紝浠?鍙傝冭祫鏂?/font> 涓鑺備腑鍒楀嚭鐨?URL 涓笅杞?Python Ant 浠誨姟搴擄紙pyAntTasks.jar錛夈傜劧鍚庯紝灝?JAR 鏂囦歡鎷瘋礉鍒?Eclipse 鐨?Ant 鎻掍歡涓嬬殑 lib 鐩綍涓傝繖搴旇鏄?Eclipse 瀹夎鐩綍涓嬪艦濡?plugins/org.apache.ant_1.5.3 鐨勫瓙鐩綍銆? Python Ant 浠誨姟搴撴嫹璐濆畬姣曚箣鍚庯紝蹇呴』鍦?Eclipse 涓惎鐢ㄥ簱銆傞夋嫨 Window > Preferences錛岀劧鍚庨夋嫨 Ant > Runtime銆傚皢 Ant Home Entries 灞曞紑錛屽叾涓彲鐪嬪埌 Eclipse 浣跨敤鐨勫簱錛圝AR 鏂囦歡錛夊垪琛ㄣ傞夋嫨鈥?Add JAR鈥濓紝鐒跺悗浠?Eclipse Ant 鎻掍歡鐨?lib 鐩綍涓夋嫨 Python Ant JAR 鏂囦歡錛屽氨鍙互灝嗗垰鍒氭嫹璐濈殑 Python Ant JAR 鏂囦歡鍔犲叆搴撳垪琛ㄤ腑錛堣鍙傞槄鍥?8錛夈? 鎮ㄧ幇鍦ㄥ簲璇ヨ兘澶熷垱寤哄拰榪愯鍖呭惈 Python 浠誨姟鐨?Ant 鏋勫緩鑴氭湰浜嗐備笅闈㈣繘鍏ユ瀯寤鴻剼鏈唴閮紒 鎴戝皢閫愭浠嬬粛濡備綍鍒涘緩涓涓畝鍗曠殑 Python 鏋勫緩鑴氭湰錛堣鍙傞槄娓呭崟 1錛夈傚畬鏁寸殑鏋勫緩鑴氭湰 build.xml 鍙粠 feedParserTest 欏圭洰鐨勯《灞傜洰褰曚腑鎵懼埌銆? 鍏堜粙緇嶄竴涓彧緙栬瘧 Python 鏍蜂緥浠g爜鐨勬瀯寤鴻剼鏈?lt;project> 鏍囩鎬繪槸鏋勫緩鑴氭湰鐨勬牴鏍囩銆?lt;taskdef> 鏍囩澹版槑鍦ㄦ暣涓瀯寤鴻剼鏈腑浣跨敤鐨?Python 浠誨姟銆傚湪鏋勫緩鑴氭湰鐨勫簳閮紝鍙互瀹氫箟 瑕佽繍琛屾瀯寤鴻剼鏈紝鍙粠 Eclipse 涓墦寮瀹冦侲clipse 鍏鋒湁鍐呯疆鐨?Ant 鏋勫緩鑴氭湰緙栬緫鍜屾祻瑙堝姛鑳姐侽utline 瑙嗗浘鍙互鏄劇ず鍑烘瀯寤鴻剼鏈殑緇撴瀯銆傚湪 Navigator 瑙嗗浘涓紝閫夋嫨璇ユ瀯寤鴻剼鏈紝鐢ㄥ彸閿偣鍑伙紝鐒跺悗閫夋嫨鈥淩un Ant...鈥濄傞夋嫨 鎺ヤ笅鏉ュ皢鍚戞瀯寤鴻剼鏈腑鍔犲叆鏂扮殑鐩爣錛岀敤浜庢墽琛?Python 鑴氭湰錛堣鍙傞槄娓呭崟 2錛夈傚湪鏈緥涓紝鍙互灝?RSS URL 浣滀負鍙傛暟鏉ユ墽琛?feedparser.py 鑴氭湰銆? 涓婇潰鐨勭洰鏍囦互 RSS URL 涓哄敮涓鐨勫弬鏁版潵鎵ц feedparser.py 鑴氭湰銆傝鐩爣澹版槑涓轟緷璧栦簬 Python 鐨?API 鏂囨。緙栧埗鏈哄埗涓?Java 鎶鏈腑鐨?JavaDoc 緋葷粺綾諱技錛岀О涓?PyDoc銆傚湪鏋勫緩鑴氭湰涓姞鍏ユ竻鍗?3 涓垪鍑虹殑濡備笅 XML 鐗囨柇錛屽彲涓烘墍鏈夌殑 Python 妯″潡鐢熸垚 PyDoc銆?
絎?8 琛屽紑濮嬫槸 py-doc 浠誨姟銆傚鍓嶆墍榪幫紝鎮ㄤ紶鍏ョ敓鎴?pydoc 榪囩▼涓墍浣跨敤鐨? 絎?9 鑷崇 11 琛屽畾涔変簡鍦ㄧ敓鎴愭枃。鐨剺q囩▼涓簲璇ュ鐞嗗摢浜?Python 婧愭枃浠躲傛枃浠墮泦鏄?Ant 鑴氭湰涓氱敤鐨勭粨鏋勶紝鍙敤浜庡畾涔夋墍鎿嶄綔鐨勪竴緇勬枃浠躲傝繖鏄竴縐嶅緢寮哄ぇ鐨勭壒鎬э紝瀹冧嬌鎮ㄨ兘澶熼氳繃鍚嶅瓧妯″紡銆佸竷灝旈昏緫鍜屾枃浠跺睘鎬ф潵閫夋嫨鎵瑕佹搷浣滅殑鏂囦歡銆侫nt 鏂囨。涓湁榪欐柟闈㈢殑瀹屾暣鎻忚堪銆傛湰渚嬩腑閫掑綊閫夋嫨浜嗏渟rc鈥濈洰褰曚笅鐨勬墍鏈夋枃浠躲? Python 涓叿鏈夋爣鍑嗙殑鍗曞厓嫻嬭瘯妗嗘灦錛堜粠 Python 2.3 寮濮嬨傚湪 Python 2.2 涓繖鍙槸鍙夋ā鍧楋級錛屼笌 Java jUnit 妗嗘灦鍗佸垎綾諱技銆傛祴璇曠敤渚嬬殑緇撴瀯涓?jUnit 閲囩敤鐩稿悓鐨勬柟寮忋傛瘡涓涓緟嫻嬭瘯鐨勭被鍜屾ā鍧楅氬父閮藉叿鏈夎嚜宸辯殑嫻嬭瘯綾匯傛祴璇曠被涓寘鍚祴璇曡緗紙fixture錛夛紝瀹冧滑鍦? 涓婅堪娓呭崟鏄疄鐜?feedparser 妯″潡鍩烘湰嫻嬭瘯鍔熻兘鐨勬祴璇曠被銆傚畬鏁寸殑嫻嬭瘯綾昏 feedParserTest 欏圭洰涓嬬殑 src/feedparserTest/FeedparserTest.py銆? 瑕佺嫭绔嬭繍琛岃嫻嬭瘯綾伙紝鍙互鎸夊墠闈㈡墍璇寸殑鐩稿悓鏂瑰紡榪愯 FeedparserTest.py 妯″潡銆傚湪 Eclipse Navigator 瑙嗗浘涓夋嫨 FeedparserTest.py錛岀劧鍚庨氳繃 Python > Run 榪愯銆傛鏃舵樉紺哄惎鍔ㄩ厤緗獥鍙c傞櫎 Base 鐩綍涔嬪錛屽叾浠栭兘淇濇寔緙虹渷鍊煎嵆鍙侭ase 鐩綍蹇呴』鏄?feedParserTest 欏圭洰鐨勭洰褰曪紝榪欐牱鎵嶈兘鍦ㄥ綋鍓嶇洰褰曚笅鎵懼埌 RSS 鏂囦歡錛坱estData/developerworks.rss錛夈備慨鏀?base 鐩綍鐨勮緗紝鐒跺悗鐐瑰嚮鈥淩un鈥濄傝緭鍑轟俊鎭樉紺哄湪 Console 涓娿? 鎮ㄤ篃璁稿笇鏈涙垜浠紪鍐欑殑鎵鏈夊崟鍏冩祴璇曢兘鑳藉浣滀負鏋勫緩鐨勪竴閮ㄥ垎鑷姩鎵ц銆傚皢涓嬮潰娓呭崟 5 鎵紺虹殑鏋勫緩鐗囨柇鍔犲叆鏋勫緩鑴氭湰渚垮彲瀹炵幇銆? 絎竴琛屾槸鐩爣澹版槑錛岃繖涓庡叾浠栫殑鑴氭湰鐩稿悓銆傜 2 鑷崇 6 琛岃皟鐢?py-test 浠誨姟銆傝繖閮ㄥ垎浠g爜灝嗗湪鈥渟rc鈥濈洰褰曚笅鏌ユ壘鎵鏈変互鈥淭est.py鈥濈粨灝劇殑鎵鏈夋枃浠訛紝騫惰繍琛屾墍鏈夋祴璇曘? 榪愯鐩爣鐨勬柟娉曟槸鍏堣繍琛屾瀯寤鴻剼鏈紝鍐嶉夋嫨鎵ц鈥渢ests鈥濈洰鏍囥傝鐩爣灝嗚繍琛屾墍鏈変互鈥淭est.py鈥濈粨灝劇殑嫻嬭瘯鐢ㄤ緥錛屾湰渚嬩腑浠呮湁 FeadparserTest.py銆? Eclipse 鍜?PyDev 鎻掍歡鐨勭粨鍚堬紝浠ュ強 Apache Ant 涓?Python Ant 浠誨姟涓璧蜂嬌鐢紝鍙互涓?Python 寮鍙戞彁渚涘畬鍏ㄩ泦鎴愮殑寮鍙戠幆澧冨拰鏋勫緩/閮ㄧ講宸ュ叿銆傝繖浜涘伐鍏峰皻鍦ㄥ紑鍙戣繃紼嬩腑錛屽洜姝よ緇忓父鏌ョ湅鏄惁鏈夋洿鏂幫紝濡傛灉鎮ㄨ寰楃壒鍒笇鏈涚湅鍒版煇縐嶇壒鎬э紝鍙互鍗瘋搗琚栫鑷姏鏇寸敓銆?
Ron Smith 鏄?RPS Technologies, Inc 鐨勫垱濮嬩漢銆傝繖鏄竴瀹惰蔣浠跺紑鍙戜笌杞歡欏鵑棶鍏徃錛屾婚儴浣嶄簬鑺濆姞鍝ュ湴鍖恒俁on Smith 涓哄鎴鋒彁渚涘熀浜?J2EE 鐨勪紒涓氬簲鐢ㄧ▼搴忓彂鏂歸潰鐨勫挩璇紝鍚屾椂涔熷湪 RPS Technologies 鍐呴儴寮鍙戣蔣浠朵駭鍝併傚彲浠ラ氳繃 ron.smith@rpstechnologies.net涓?Ron 鑱旂郴銆?
python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ錛?/a>
i = 1
聽 鈥滅鍊艱鍙ユ槸鐢ㄦ潵灝嗗悕瀛楃粦瀹氾紙鎴栭噸鏂扮粦瀹氾級鍒版煇涓璞$殑鎿嶄綔錛岃屼笖瀹冧篃鍙敤鏉ヤ慨鏀瑰彲鍙樺璞$殑灞炴ф垨
瀵硅薄涓墍鍖呭惈鐨勬垚鍛樸傗?/p>
>>> b = a*4
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b
聽 鈥滀竾鐗╃殕瀵硅薄鈥?Everything is object)錛岃繖鏄痯ython榪欑闈㈠悜瀵硅薄璇█鎵鍊″鐨勭悊蹇點傚湪鎴戜滑鐔熸倝鐨凜++涓紝1鍙槸涓涓暣鍨嬫暟錛岃屼笉鏄竴涓璞°備絾鍦╬ython涓紝1鍗存槸涓涓疄瀹炲湪鍦ㄧ殑瀵硅薄錛屾偍鍙互鐢╠ir(1)鏉ユ樉紺哄畠鐨勫睘鎬с?/p>
聽* 鍞竴鐨勬爣璇嗙爜(identity)
聽* 綾誨瀷
聽* 鍐呭錛堟垨縐頒負鍊鹼級
聽pass
...
>>> obj = c1()
>>> obj
<__main__.c1 instance at 0x00AC0738>
>>> id(obj)
11274040
7957136
聽 鍏剁粨鏋滄槸False錛屾槸鍥犱負榪欐槸涓や釜涓嶅悓鐨勫璞★紝瀛樺偍鍦ㄥ唴瀛樹腑鐨勪笉鍚屽湴鏂廣?/font>
聽 鍏剁粨鏋滄槸True錛屾槸鍥犱負榪欎袱涓笉鍚岀殑瀵硅薄鏈夌潃鐩稿悓鐨勫箋?/font>
聽* 娌℃湁鎴栬呮嫢鏈夊涓柟娉?br />聽* 娌℃湁鎴栬呮湁澶氫釜鍚嶅瓧
聽 鍚嶅瓧鏄涓涓璞$殑縐板懠錛屼竴涓璞″彲浠ュ彧鏈変竴涓悕瀛楋紝涔熷彲浠ユ病鏈夊悕瀛楁垨鍙栧涓悕瀛椼備絾瀵硅薄鑷繁鍗翠笉鐭ラ亾鏈夊灝戝悕瀛楋紝鍙粈涔堬紝鍙湁鍚嶅瓧鏈韓鐭ラ亾瀹冩墍鎸囧悜鐨勬槸涓粈涔堝璞°傜粰瀵硅薄鍙栦竴涓悕瀛楃殑鎿嶄綔鍙綔鍛藉悕錛宲ython灝嗚祴鍊艱鍙ヨ涓烘槸涓涓懡鍚嶆搷浣滐紙鎴栬呯О涓哄悕瀛楃粦瀹氾級銆?/p>
聽
3. 緇戝畾
聽 濡備笂鎵璁茬殑錛岀粦瀹氬氨鏄皢涓涓璞′笌涓涓悕瀛楄仈緋昏搗鏉ャ傛洿紜垏鍦拌錛屽氨鏄鍔犺瀵硅薄鐨勫紩鐢ㄨ鏁般備紬鎵鍛ㄧ煡錛孋++涓竴澶ч棶棰樺氨鏄唴瀛樻硠婕?-- 鍗沖姩鎬佸垎閰嶇殑鍐呭瓨娌℃湁鑳藉鍥炴敹錛岃岃В鍐寵繖涓闂鐨勫埄鍣ㄤ箣涓灝辨槸寮曠敤璁℃暟銆俻ython灝遍噰鐢ㄤ簡榪欎竴鎶鏈疄鐜板叾鍨冨溇鍥炴敹鏈哄埗銆?br />聽
聽 python涓殑鎵鏈夊璞¢兘鏈夊紩鐢ㄨ鏁般?/p>
聽 寮曠敤璁℃暟鍑忎竴銆?br />* "i"鎵鎸囧悜鐨勮佸璞$殑鍊煎茍娌℃湁鏀瑰彉銆?br />* 榪欏氨鏄負浠涔堝湪python涓病鏈?+銆?-榪欐牱鐨勫崟鐩繍綆楃鐨勪竴涓師鍥犮?/p>
聽 瀵硅薄鐨勫紩鐢ㄨ鏁板湪涓嬪垪鎯呭喌涓嬩細澧炲姞錛?br />聽* 璧嬪兼搷浣?br />聽* 鍦ㄤ竴涓鍣紙鍒楄〃錛屽簭鍒楋紝瀛楀吀絳夌瓑錛変腑鍖呭惈璇ュ璞?/p>
聽* 瀵硅薄鐨勪竴涓悕瀛楄緇戝畾鍒板彟澶栦竴涓璞?br />聽* 瀵硅薄浠庡寘鍚畠鐨勫鍣ㄤ腑縐婚櫎
聽* 鍚嶅瓧琚樉紺哄湴鐢╠el閿姣侊紙濡傦細del i錛?/p>
>>> li2 = li1
>>> li1[1] = 16
>>> print li2
[7, 16, 9, 10]
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]
>>> print b
[{'g': 1}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 1}, {'g': 1}, {'g': 1}]
>>> b = [a]*4
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]
>>> print a
{'g': 2}
>>> b = [a] + [a] + [a] + [a]
>>> b[0]['g'] = 2
>>> print b
[{'g': 2}, {'g': 2}, {'g': 2}, {'g': 2}]
>>> print a
{'g': 2}
>>> a = [1, 2, 3]
>>> def foo(par):
...聽par[1] = 10
...
>>> foo(a)
>>> print a
[1, 10, 3]
聽 浣跨敤緙虹渷鍙傛暟錛屾槸鎴戜滑鍠滅埍鐨勪竴縐嶄綔娉曘傝繖鍙互鍦ㄨ皟鐢ㄨ鍑芥暟鏃惰妭鐪佷笉灝戠殑鍑婚敭嬈℃暟錛岃屼笖浠g爜涔熸樉寰楁洿鍔犵畝媧併傛洿閲嶈鐨勬槸瀹冧粠鏌愮鎰忎箟涓婁綋鐜頒簡榪欎釜鍑芥暟璁捐鐨勫垵琛楓?br />聽 浣嗘槸python涓殑緙虹渷鍙傛暟錛屽嵈闅愯棌鐫涓涓巹鏈猴紝鍒濆鑰呰偗瀹氫細鍦ㄤ笂闈㈡牻璺熷ご錛岃屼笖榪欎釜閿欒闈炲父闅愮銆傚厛鐪嬬湅涓嬮潰榪欎釜渚嬪瓙錛?/p>
>>> def foo(par=[]):
...聽par.append(0)
...聽print par
...聽
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎竴嬈¤皟鐢?br />[0]
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎簩嬈¤皟鐢?br />[0, 0]
['__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']
([0, 0],)
>>> foo()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 # 絎笁嬈¤皟鐢?br />[0, 0, 0]
>>> foo.func_defaults聽聽聽聽聽聽聽聽聽 # 鍐嶆潵鐪嬬湅榪欎釜灞炴?br />([0, 0, 0],)
...聽print par1, def1, def2, def3
...
>>> fooM.func_defaults
(1, [], 'str')
聽 鍑芥暟fooM鍐呯殑鍚嶅瓧def1灝變細緇戝畾鍒癴unc_defaults涓殑絎竴涓璞★紝def2緇戝畾鍒扮浜屼釜錛宒ef3鍒欐槸絎笁涓?br />鎵浠ユ垜浠湅鍒板湪鍑芥暟foo涓嚭鐜扮殑绱姞鐜拌薄錛屽氨鏄敱浜巔ar緇戝畾鍒扮己鐪佸弬鏁板璞′笂錛岃屼笖瀹冩槸涓涓彲鍙樺璞★紙鍒楄〃錛夛紝par.append(0)灝變細姣忔鏀瑰彉榪欎釜緙虹渷鍙傛暟瀵硅薄鐨勫唴瀹廣?/p>
...聽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],)
...聽if par is None:
...聽聽par = []
...聽par.append(0)
...聽print par
鍙傛暟瀵硅薄涓娿?/p>
聽 * python鏄竴縐嶇函綺圭殑闈㈠悜瀵硅薄璇█銆?br />聽 * 璧嬪艱鍙ユ槸鍚嶅瓧鍜屽璞$殑緇戝畾榪囩▼銆?br />聽 * 鍑芥暟鐨勪紶鍙傛槸瀵硅薄鍒頒笉鍚屽悕瀛楃┖闂寸殑緇戝畾銆?/p>
璇勮
re: python鐨勫璞′笌鍚嶅瓧緇戝畾錛堣漿璐達紝姝ゆ枃鐢氬ソ錛?2005-12-28 16:43 jarodzz
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
>>> def foolyou(a):
print id(a)
a=a+1
print id(a)
>>> b
1
>>> foolyou(b)
148479408
148479396
>>> id(b)
148479408聽聽鍥炲聽聽
]]>
]]>
[3*x for x in vec if x > 3]
[x*y for x in vec1 for y in vec2]
......
there expression can used to instead the functional programming tools such as map() ,filter(),reduce()..
Note Two : Some functions in the modules often聽 be made used of
1.strip()聽:聽 Return a copy of the string s with leading and trailing聽whitespace removed.
>>> test_str = '聽聽 I Love Python聽 '
>>>聽string.strip(test_str)
'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!
2. str() : can convert the format like int ,long , float ... into string format
>>> num_1 = 3.14
>>> num_2 = 0.618
>>> str(num_1) , str(num_2)
'3.14' '0.618'
3.dict()
dict() -> new empty dictionary.
dict(mapping) -> new dictionary initialized from a mapping object's
聽聽聽 (key, value) pairs.
dict(seq) -> new dictionary initialized as if via:
聽聽聽 d = {}
聽聽聽 for k, v in seq:
聽聽聽聽聽聽聽 d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
聽聽聽 in the keyword argument list.聽 For example:聽 dict(one=1, two=2)
e.g聽
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
dict([(x, x**2) for x in (2, 4, 6)])聽聽聽聽
dict(sape=4139, guido=4127, jack=4098)
4. enumerate()
Return an enumerate object.聽 iterable must be an other object that supports
iteration.聽 The enumerate object yields pairs containing a count (from
zero) and a value yielded by the iterable argument.聽 enumerate is useful
for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
Code聽 show:
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...聽聽聽聽 print i, v
...
0 tic
1 tac
2 toe
5 zip()
Return an enumerate object.聽 iterable must be an other object that supports
iteration.聽 The enumerate object yields pairs containing a count (from
zero) and a value yielded by the iterable argument.聽 enumerate is useful
for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
Code Show:>>> 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.
6.sorted()
Code Show:>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print f
...
apple
banana
orange
pear
to be continued......
Note Three : simple statements
7 The yield statementgenerators feature has been enabled. It will always be enabled in Python 2.3. This __future__ import statement can be used to enable the feature:
from __future__ import generators
8 The raise statementraise_stmt ::= "raise" [expression ["," expression ["," expression]]] None as the value of omitted expressions. The first two objects are used to determine the type and value of the exception.
None.
None, 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.
None, it must be a traceback object (see section聽3.2), 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 None, a TypeError exception is raised. The three-expression form of raise is useful to re-raise an exception transparently in an except clause, but raise with no expressions should be preferred if the exception to be re-raised was the most recently active exception in the current scope.
9 The try statementtry_stmt ::= try_exc_stmt | try_fin_stmt try_exc_stmt ::= "try" ":" suite ("except" [expression ["," target]] ":" suite)+["else" ":" suite]try_fin_stmt ::= "try" ":" suite "finally" ":" suite sys.exc_type receives the object identifying the exception; sys.exc_value receives the exception's parameter; sys.exc_traceback receives a traceback object (see section聽3.2) identifying the point in the program where the exception occurred. These details are also available through the sys.exc_info() function, which returns a tuple (exc_type, exc_value, exc_traceback). 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.
]]>
]]>
澶寸柤浜嗗緢涔?eclipse涓棤娉曟甯告洿鏂皃ydev銆傛垜鍙堝洖澶存鏌ワ紝鍩熷悕鏄紜殑錛屼絾鏄繕鏄嚭鐜伴敊璇?br />鎯充簡寰堜箙錛屽湪緗戜笂鍒板鎵懼叧浜庢綾婚棶棰樼殑鏂囩珷錛屼絾鏄敹鑾蜂笉澶э紝鍙互璇村嚑涔庢壘涓嶅埌涔嬬被鏂囩珷銆?br />騫歌繍鐨勪簨 錛屾垜姝e湪瀵繪壘鍏朵粬鎻掍歡鏃跺欏拰瀵熺湅鍏朵粬python寮鍙戝伐鍏風殑鏃跺欙紝鏃犳剰涓偣鍑轟簡IBM 鐨勪笅闈㈣繖涓〉闈€?br />欏挎椂鎴戝氨鎭嶇劧澶ф偀錛屾墠鍙戠幇鏃犳硶鏇存柊鐨勫師鍥狅細
鍥犱負鍏徃鐨勫皝鎺変簡鍑犱釜绔彛錛岄槻姝嬌鐢ㄩ潪宸ヤ綔鑼冨洿鍐呯殑濞變箰杞歡鎴栧叾浠栫殑鑱婂ぉ杞歡錛?br />鑰宔clipse鐨勬洿鏂版ā寮忓熀浜庡熀浜嶧TP妯″紡鐨勩?br />褰撶劧榪欐槸閫犳垚鏃犳硶鏇存柊鐨勫師鍥犱箣涓錛屽彲鑳借繕鏈夊叾浠栧師鍥犱絾鏄垜娌℃兂鍒幫紝涔熸病閬囪榪囥?br />
quote:
涓嬭澆鏂規硶錛欶TP銆丠TTP 鍜屼笅杞藉姪鎵?/span>
聽
r 5.01 鍙婁互涓婄増鏈湁鏃惰兘澶熸仮澶嶄腑鏂殑 HTTP 涓嬭澆銆侶TTP 涓嬭澆鑳藉絀胯繃闃茬伀澧欙紝鍥犱負瀹冧嬌鐢ㄤ簡绔彛鍙?80 鏉ヨ繘琛屾暟鎹紶杈撱備互涓嬩笅杞借涓哄拰閿欒鍙兘鏄亣鍒頒簡闃茬伀澧欓棶棰橈細
]]>
鐢?Eclipse 鍜?Ant 榪涜 Python 寮鍙?/h1>
鏂囨。閫夐」
鏈鏂版帹鑽?/td>
Python 鏄竴縐嶉潪甯哥伒媧誨己澶х殑鍔ㄦ佽剼鏈紪紼嬭璦錛屽叿鏈夊畬鏁寸殑闈㈠悜瀵硅薄鐗規с備紬澶氱殑鏀寔鑰呮寚鍑猴紝Python 璇█涓庡叾浠栬璦鐩告瘮鑳芥洿蹇洿鏈夋晥鍦拌〃杈懼嚭浠栦滑鐨勬剰鍥俱備絾鏄粠 Java 鎶鏈劉 鎴?Microsoft廬 .NET 鍒氬垰杞埌 Python 鐨勪漢浼氬彂鐜幫紝鍔熻兘涓板瘜鑰岀簿鑷寸殑 IDE 鍜屽紑鍙戝伐鍏烽兘涓嶈浜嗐傞偅浜涘紑鍙戜漢鍛樺彲浠ヤ粠浠栦滑鐔熸倝鐨?Java 寮鍙戝伐鍏蜂腑鎵懼埌瑙e喅鏂規銆傛湰鏂囩潃閲嶄粙緇嶄簡濡備綍浣跨敤鍩轟簬 Java 鎶鏈殑嫻佽寮鍙戝伐鍏?Eclipse 鍜?Ant 榪涜 Python 寮鍙戙?/blockquote>
鍥為〉棣?/font>
鍥為〉棣?/font>
鍥?1. PyDev 閫夐」紿楀彛
鍥?2. PyDev Debug 閫夐」
鍥?3. Python 緙栬緫鍣?/b>
PYTHONPATH 鐩綍涓級鐨勫悓鏃舵寜涓?Control 閿紝PyDev 灝辮兘鏄劇ず鍑轟竴涓秴閾炬帴錛岃繖鏍鋒偍鍙互鍦ㄥ鍏ュ簱鎴栧嚱鏁扮殑婧愪唬鐮佷箣闂村鑸傝娉ㄦ剰錛屼負浜嗗湪鎮ㄨ嚜宸辯殑婧愪唬鐮佷腑璺ㄦā鍧椾嬌鐢ㄨ鐗規э紙浠庝竴涓ā鍧楅摼鎺ュ埌鍙︿竴涓ā鍧楋級錛屽繀欏諱慨鏀?PYTHONPATH 鐜鍙橀噺錛屽湪鍏朵腑鍔犲叆榪欎簺妯″潡錛岃繖鏍?PyDev 灝卞彲浠ユ壘鍒板畠浠簡銆?
鍥?4. PyDev 緙栬緫鍣ㄧ殑鍏朵粬鐗規?/b>
鍥?5. Python 鍚姩閰嶇疆
鍥?6. Python 璋冭瘯鍣?/b>
鍥為〉棣?/font>
鍥?7. Python 浜や簰鏂瑰紡閰嶇疆
PYTHONPATH 鐜鍙橀噺涓?
鍥為〉棣?/font>
鍥?8. 鍚?classpath 涓姞鍏?Python Ant 浠誨姟
娓呭崟 1. 鐢ㄤ簬緙栬瘧 Python 婧愪唬鐮佺殑鏋勫緩鑴氭湰鐗囨柇
<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>
compile 鐩爣銆傜洰鏍囧厓绱犲唴閮ㄦ槸 compile 榪愯鏈熼棿鎵ц鐨勪換鍔°傜壒鍒殑鏄?py-compile 浠誨姟錛屽畠璐熻矗浠?src 鐩綍寮濮嬶紝緙栬瘧鎵鏈夌殑 Python 浠g爜銆傝浠誨姟浼氶掑綊閬嶅巻鎵鏈夌殑瀛愮洰褰曪紝騫剁紪璇戞墍鏈夌殑 Python 妯″潡銆傝剼鏈腑娌℃湁閲囩敤灝?src 鐩綍紜紪鐮佸埌璋冪敤涔嬪鐨勬柟寮忥紝鑰屾槸鍦ㄦ瀯寤鴻剼鏈腑瀹氫箟浜嗙О涓?src.dir 鐨勫睘鎬с傜劧鍚庯紝鍦ㄩ渶瑕佷嬌鐢ㄨ繖涓洰褰曞悕鐨勬椂鍊欙紝灝卞彲浠ラ氳繃 ${src.dir} 鏉ュ紩鐢ㄣ?compile 鐩爣錛岀劧鍚庣偣鍑燴淩un鈥濄傛瀯寤鴻剼鏈墽琛岃繃紼嬩腑鐨勮緭鍑轟俊鎭簲璇ユ樉紺哄湪 Console 瑙嗗浘涓紝琛ㄧず榪愯鎴愬姛銆?
娓呭崟 2. 榪愯 feedparser 鑴氭湰鐨勬瀯寤鴻剼鏈墖鏂?/b>
<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>
compile 鐩爣錛屾墍浠ュ悗鑰呭皢棣栧厛鎵ц銆傚疄闄呬笂榪欎竴姝ュ茍涓嶆槸寰堝繀瑕侊紝鍥犱負 Python 浼氭牴鎹渶瑕佽嚜鍔ㄧ紪璇戞簮浠g爜銆傚鏋滄偍鎵ц run.feedparser 鐩爣錛屽氨浼氳繍琛?feedparser.py 鑴氭湰錛屽悓鏃跺皢 RSS 鐨勫唴瀹硅緭鍑哄埌 Console 涓?
娓呭崟 3. 鐢ㄤ簬鐢熸垚 PyDoc 鐨勬瀯寤鴻剼鏈墖鏂?/b>
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>
浠庡涓婅堪 pydoc 鐩爣鐨勮В鏋愬彲鐪嬪嚭錛岀 7 琛屽0鏄庝簡鐩爣鍚嶇О錛屽茍鎸囧嚭瀹冧緷璧栦簬
init 鍜?compile 鐩爣銆傝繖鎰忓懗鐫鍦ㄨ繍琛?pydoc 鐩爣涔嬪墠錛孉nt 蹇呴』淇濊瘉 init 鍜?compile 鐩爣宸茬粡榪愯錛屽鏋滄病鏈夛紝鍒欓鍏堣繍琛岃繖涓や釜鐩爣銆?pydoc 鐩爣鎵渚濊禆鐨?init 鐩爣鍦ㄧ 3 鑷崇 5 琛屽畾涔夈?init 鐩爣浠呬粎鍒涘緩浜嗕竴涓瓨鏀?PyDoc API 鏂囨。鏂囦歡鐨勭洰褰曘傚鍓嶆墍榪幫紝瑕佷負鎵鐢熸垚鏂囨。鐨勪繚瀛樹綅緗畾涔変竴涓睘鎬э紝鍚嶄負 pydoc.dir銆?PYTHONPATH 銆?destdir 灞炴у憡璇?py-doc 浠誨姟灝嗙敓鎴愮殑 HTML 鏂囨。杈撳嚭鍒頒綍澶勩?setUp 鍑芥暟涓垵濮嬪寲銆傛瘡涓涓祴璇曢兘緙栧啓涓烘祴璇曠被涓殑涓涓嫭绔嬬殑嫻嬭瘯鍑芥暟銆倁nittest 妗嗘灦浼氬湪嫻嬭瘯鍑芥暟涔嬮棿寰幆寰澶嶏紝鍏堣皟鐢?setUp 銆佸啀嫻嬭瘯鍑芥暟銆佺劧鍚庢竻闄わ紙 tearDown 錛夋祴璇曞嚱鏁般傝鍙傞槄娓呭崟 4 涓殑鏍蜂緥銆?
娓呭崟 4. Python 鍗曞厓嫻嬭瘯妯″潡
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()
setUp 鍑芥暟璐熻矗鍑嗗鏁翠釜嫻嬭瘯榪囩▼涓渶瑕佷嬌鐢ㄧ殑嫻嬭瘯瑁呯疆錛屽湪鏈緥涓彧鏈夋祴璇曠敤鐨?RSS 鏂囦歡鐨勭洰褰曪紝嫻嬭瘯鍑芥暟灝嗗鍏惰繘琛岃В鏋愩?testParse09Rss 鏄湡姝g殑嫻嬭瘯鍑芥暟銆傝繖涓嚱鏁拌皟鐢?feedparser.parse 鍑芥暟錛屼紶閫掓祴璇曠敤鐨?RSS 鏂囦歡錛岃緭鍑鴻В鏋愮粨鏋滐紝騫墮氳繃 TestCase 綾葷殑 assert 鍑芥暟鎵ц鍩烘湰鐨勬鏌ョ粺浣溿傚鏋滀換浣?assert 鐨勬眰鍊肩粨鏋滀笉鏄湡錛屾垨鏄湪鎵ц榪囩▼涓姏鍑轟換浣曞紓甯革紝unittest 灝變細鎶ュ憡涓嬈℃祴璇曞け璐ユ垨閿欒銆傛渶鍚庣殑涓よ璐熻矗鍦ㄨ繖涓祴璇曠被鍐呴儴榪愯嫻嬭瘯錛屾柟娉曟槸鐩存帴榪愯璇ユā鍧楀嵆鍙?
娓呭崟 5. 鎵ц鍗曞厓嫻嬭瘯鐨勬瀯寤鴻剼鏈墖鏂?/b>
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>
PYTHONPATH 璁劇疆涓衡渟rc鈥濓紝嫻嬭瘯鎵ц鐨勫綋鍓嶅伐浣滅洰褰曞氨鏄綋鍓嶇洰褰曪紙鈥?鈥欙級銆?
鍥為〉棣?/font>
鍥為〉棣?/font>
鍥為〉棣?/font>
]]>
Note One錛歛bout sequences and lists
搴忓垪鏄笉鍙彉鍒楄〃銆備竴鏃﹀垱寤轟簡涓涓簭鍒楀氨涓嶈兘浠ヤ換浣曟柟寮忔敼鍙樺畠銆?/font>
渚?1.21. 瀹氫箟搴忓垪
>>>
t = ("a", "b", "mpilgrim", "z", "example")
>>>
t
('a', 'b', 'mpilgrim', 'z', 'example')
>>>
t[0]
'a'
>>>
t[-1]聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
'example'
>>>
t[1:3]聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
('b', 'mpilgrim')
渚?1.22. 搴忓垪娌℃湁鏂規硶聽
>>>聽
t
('a', 'b', 'mpilgrim', 'z', 'example')
>>>聽
t.append("new")聽聽
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'
>>>聽
t.remove("z")聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'remove'
>>>聽t.index("example")聽聽
Traceback (innermost last):
聽 File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'index'
>>>"z"聽in t聽聽
1聽聽聽
|
|
浣犱笉鑳藉悜搴忓垪澧炲姞鍏冪礌銆傚簭鍒楁病鏈?append 鎴?extend 鏂規硶銆?/font> |
|
|
浣犱笉鑳戒粠搴忓垪涓櫎鎺夊厓绱犮傚簭鍒楁病鏈?remove 鎴?tt> pop 鏂規硶銆?/font> |
|
|
浣犱笉鑳藉湪搴忓垪涓煡鎵懼厓绱犮傚簭鍒楁病鏈?index 鏂規硶銆?/font> |
|
|
鐒惰岋紝浣犲彲浠ヤ嬌鐢?in 鏉ョ湅涓鐪嬫槸鍚︿竴涓厓绱犲瓨鍦ㄤ簬搴忓垪涓?/font>銆?/font> |
閭d箞搴忓垪鏈変粈涔堝ソ澶勫憿錛?/font>
Note Two : Mapping in the Lists
鍒楄〃鏄犲皠浠嬬粛聽
聽聽聽聽聽聽
>>>聽聽li = [1, 9, 8, 4]
>>>聽聽[elem*2 for elem in li]聽聽聽聽聽 ![]()
[2, 18, 16, 8]
>>>聽聽li聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽![]()
[1, 9, 8, 4]
Python鏀寔涓縐嶆湁瓚g殑璇硶錛屽畠鍏佽浣犲揩閫熷畾涔夊崟琛岀殑鏈灝忓嚱鏁般傝繖浜涘彨鍋?lambda 鐨勫嚱鏁版槸浠嶭isp涓熺敤鏉ョ殑錛屽彲浠ヨ鐢ㄥ湪浠諱綍闇瑕佸嚱鏁扮殑鍦版柟銆?/font>
鍑轟簬鍘嗗彶鐨勫師鍥狅紝lambda 鍑芥暟鐨勮娉曚笌閫氬父鐨勫嚱鏁版湁浜涚粏寰殑涓嶅悓銆?/font>
渚?2.20. lambda 鍑芥暟浠嬬粛
>>>
def
f(x):聽聽聽聽聽聽聽聽聽
...聽聽聽聽
return x*2
...聽聽聽聽
>>>
f(3)
6
>>>
g = lambda x: x*2聽
>>>
g(3)
6
>>>
(lambda x: x*2)(3)
6
鎬諱箣錛?lambda 鍑芥暟鏄竴涓彲浠ユ帴鏀朵換鎰忓涓弬鏁?鍖呮嫭 鍙夊弬鏁?/font> )騫朵笖榪斿洖鍗曚釜琛ㄨ揪寮忓肩殑鍑芥暟銆?lambda 鍑芥暟涓嶈兘鍖呭惈鍛戒護錛屽畠浠墍鍖呭惈鐨勮〃杈懼紡涓嶈兘瓚呰繃涓涓備笉瑕佽瘯鍥懼悜 lambda 鍑芥暟涓鍏ュお澶氱殑涓滆タ錛涘鏋滀綘闇瑕佹洿澶嶆潅鐨勪笢瑗匡紝搴旇瀹氫箟涓涓櫘閫氬嚱鏁幫紝鐒跺悗鎯寵瀹冨闀垮氨澶氶暱銆?/font>
|
|
|
| lambda 鍑芥暟鏄鏍奸棶棰樸備笉涓瀹氶潪瑕佷嬌鐢ㄥ畠浠紝浠諱綍鑳藉浣跨敤瀹冧滑鐨勫湴鏂癸紝閮藉彲浠ュ畾涔変竴涓垎紱葷殑鏅氱殑鍑芥暟錛岀敤瀹冩潵鏇挎崲銆傛垜灝嗗畠浠敤鍦ㄩ渶瑕佸皝瑁呯壒孌婄殑錛岄潪閲嶇敤鐨勪唬鐮佷笂錛岀敤璁稿灝忕殑涓琛屽嚱鏁頒笉浼氬紕涔辨垜鐨勪唬鐮併?/font> | |
渚?2.21. 鍦?in apihelper.py 涓殑 lambda 鍑芥暟聽
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
欏轟究榪欓噷鏈夊嚑浠朵簨鎯呴渶瑕佹敞鎰忋傞鍏堬紝鎴戜滑浣跨敤浜? and-or 鎶宸х殑綆鍗曞艦寮忥紝娌¢棶棰橈紝鍥犱負涓涓?lambda 鍑芥暟 鍦ㄤ竴涓竷灝旂幆澧冧笅 鎬諱負鐪熴?榪欏茍涓嶆剰鍛崇潃 lambda 鍑芥暟涓嶈兘榪斿洖鍋囧箋傚嚱鏁版湰韜繪槸涓虹湡錛屽畠鐨勮繑鍥炲煎彲浠ヤ負浠諱綍鍊箋?
絎簩錛屾垜浠嬌鐢ㄤ簡 split 鍑芥暟娌″甫鍙傛暟銆備綘宸茬粡鐪嬪埌榪囧畠甯?/font> 1涓垨2涓弬鏁?/font> 鐨勪嬌鐢紝浣嗘槸涓嶅甫鍙傛暟瀹冩寜絀虹櫧榪涜鍒嗗壊銆?/font>
渚?2.22. split 涓嶅甫鍙傛暟聽聽聽聽
>>>s = "this聽聽 is\na\ttest"聽聽聽聽聽聽聽聽 ![]()
>>>print s聽聽
聽this聽聽 is
a test
>>>print s.split()聽聽聽聽聽 ![]()
['this', 'is', 'a', 'test']
>>>print聽聽聽" ".join(s.split())聽聽聽聽聽聽聽 ![]()
'this is a test'聽聽聽聽
|
|
榪欐槸涓涓琛屽瓧絎︿覆錛岄氳繃杞箟瀛楃鐨勫畾涔変唬鏇夸簡 涓夐噸寮曞彿 銆?\n 鏄竴涓洖杞︼紱 \t 鏄竴涓埗琛ㄧ銆?/font> |
|
|
split 涓嶅甫鍙傛暟鎸夌┖鐧借繘琛屽垎鍓層傛墍浠ヤ笁涓┖鏍鹼紝涓涓洖杞︼紝鍜屼竴涓埗琛ㄧ閮芥槸涓鏍風殑銆?/font> |
|
|
浣犲彲浠ュ皢絀虹櫧緇熶竴鍖栵紝閫氳繃鍒嗗壊涓涓瓧絎︿覆錛岀劧鍚庣敤鍗曚釜絀烘牸浣滀負鍒嗛殧絎﹀皢鍏墮噸鏂版帴璧鋒潵銆傝繖灝辨槸 help 鍑芥暟鎵鍋氱殑錛屽皢澶氳鏂囨。瀛楃涓插悎騫舵垚鍗曡銆?/font> |
閭d箞 help 鍑芥暟鍒板簳鐢ㄨ繖浜?lambda 鍑芥暟錛?split 鍑芥暟錛屽拰 and-or 鎶宸у仛浜嗕粈涔堝憿錛?
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
processFunc 鐜板湪鏄竴涓嚱鏁幫紝浣嗗畠涓哄摢涓涓嚱鏁拌鐪?collapse 鍙橀噺鐨勫箋傚鏋?collapse 涓虹湡錛?processFunc(string) 灝嗗帇緙╃┖鐧斤紱鍚﹀垯錛?tt>processFunc(string) 灝嗚繑鍥炴湭鏀瑰彉鐨勫弬鏁般?
鍦ㄤ竴涓笉寰堝緩澹殑璇█瀹炵幇瀹冿紝璞B錛屼綘灝嗗彲鑳藉垱寤轟竴涓嚱鏁幫紝瀹冩帴鏀朵竴涓瓧絎︿覆鍜屼竴涓?collapse 鍙傛暟錛屼嬌鐢ㄤ竴涓?if 璇彞鏉ュ垽鏂槸鍚﹁鍘嬬緝絀虹櫧鎴栦笉鍘嬬緝錛岀劧鍚庤繑鍥炵浉搴旂殑鍊箋傝繖鏍鋒晥鐜囦綆錛屽洜涓哄嚱鏁板皢涓嶅緱涓嶅鐞嗘瘡縐嶅彲鑳芥э紱姣忔浣犺皟鐢ㄥ畠錛屽畠灝嗕笉寰椾笉鍦ㄧ粰鍑轟綘鎵鎯寵鐨勪笢瑗夸箣鍓嶏紝鍒ゆ柇鏄惁瑕佸帇緙╃┖鐧姐傚湪Python涓紝浣犲彲浠ュ皢閭g鍒ゆ柇閫昏緫鎷垮埌鍑芥暟澶栭潰錛岃屽畾涔変竴涓鍑忚繃鐨?lambda 鍑芥暟鏉ョ粰鍑虹‘鍒囩殑(騫朵笖鍞竴)浣犳兂瑕佺殑銆傝繖鏍峰仛鏇存湁鏁堢巼錛屾洿婕備寒錛屽茍涓旀洿灝戝鑷撮偅浜涗護浜鴻鍘岀殑(鍝︼紝鎯沖埌閭d簺鍙傛暟灝卞ご鏄?鐨勯敊璇?br />
This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts.
The repr module provides an version of repr() for abbreviated displays of large or deeply nested containers:
>>> import repr
>>> repr.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
The pprint 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:
>>> import pprint
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
... 'yellow'], 'blue']]]
...
>>> pprint.pprint(t, width=30)
[[[['black', 'cyan'],
'white',
['green', 'red']],
[['magenta', 'yellow'],
'blue']]]
The textwrap module formats paragraphs of text to fit a given screen width:
>>> 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.
The locale 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:
>>> 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'
The string module includes a versatile Template class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application.
The format uses placeholder names formed by "$" 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 "$$" creates a single escaped "$":
>>> 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.'
The substitute method raises a KeyError 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 safe_substitute method may be more appropriate -- it will leave placeholders unchanged if data is missing:
>>> 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.'
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:
>>> 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
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.
The
struct
module provides pack() and unpack() 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 "H" and "L" representing two and four byte unsigned numbers respectively):
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
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.
The following code shows how the high level threading module can run tasks in background while the main program continues to run:
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.'
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.
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 Queue module to feed that thread with requests from other threads. Applications using Queue objects for inter-thread communication and coordination are easier to design, more readable, and more reliable.
The
logging
module offers a full featured and flexible logging system. At its simplest, log messages are sent to a file or to sys.stderr:
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')
This produces the following output:
WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down
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: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
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.
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.
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 weakref 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:
>>> 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'
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.
The
array
module provides an array() 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 "H") rather than the usual 16 bytes per entry for regular lists of python int objects:
>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932
>>> a[1:3]
array('H', [10, 700])
The collections module provides a deque() 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:
>>> 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)
In addition to alternative list implementations, the library also offers other tools such as the bisect module with functions for manipulating sorted lists:
>>> 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')]
The heapq 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:
>>> 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]
The decimal module offers a Decimal datatype for decimal floating point arithmetic. Compared to the built-in float 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.
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:
>>> from decimal import *
>>> Decimal('0.70') * Decimal('1.05')
Decimal("0.7350")
>>> .70 * 1.05
0.73499999999999999
The Decimal 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.
Exact representation enables the Decimal class to perform modulo calculations and equality tests that are unsuitable for binary floating point:
>>> 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
The decimal module provides arithmetic with as much precision as needed:
>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal("0.142857142857142857142857142857142857")
The os module provides dozens of functions for interacting with the operating system:
>>> import os
>>> os.system('time 0:02')
0
>>> os.getcwd() # Return the current working directory
'C:\\Python24'
>>> os.chdir('/server/accesslogs')
Be sure to use the "import os" style instead of "from os import *". This will keep os.open() from shadowing the builtin open() function which operates much differently.
The builtin dir() and help() functions are useful as interactive aids for working with large modules like os:
>>> import os >>> dir(os) <returns a list of all module functions> >>> help(os) <returns an extensive manual page created from the module's docstrings>
For daily file and directory management tasks, the shutil module provides a higher level interface that is easier to use:
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
>>> shutil.move('/build/executables', 'installdir')
The glob module provides a function for making file lists from directory wildcard searches:
>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']
Common utility scripts often need to process command line arguments. These arguments are stored in the sys module's argv attribute as a list. For instance the following output results from running "python demo.py one two three" at the command line:
>>> import sys >>> print sys.argv ['demo.py', 'one', 'two', 'three']
The getopt module processes sys.argv using the conventions of the Unixgetopt() function. More powerful and flexible command line processing is provided by the optparse module.
The sys module also has attributes for stdin, stdout, and stderr. The latter is useful for emitting warnings and error messages to make them visible even when stdout has been redirected:
>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one
The most direct way to terminate a script is to use "sys.exit()".
The re module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:
>>> 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'
When only simple capabilities are needed, string methods are preferred because they are easier to read and debug:
>>> 'tea for too'.replace('too', 'two')
'tea for two'
The math module gives access to the underlying C library functions for floating point math:
>>> import math >>> math.cos(math.pi / 4.0) 0.70710678118654757 >>> math.log(1024, 2) 10.0
The random module provides tools for making random selections:
>>> 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
There are a number of modules for accessing the internet and processing internet protocols. Two of the simplest are urllib2 for retrieving data from urls and smtplib for sending mail:
>>> 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()
The datetime 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.
# 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
Common data archiving and compression formats are directly supported by modules including: zlib, gzip, bz2, zipfile, and tarfile.
>>> 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
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.
For example, it may be tempting to use the tuple packing and unpacking feature instead of the traditional approach to swapping arguments. The timeit module quickly demonstrates a modest performance advantage:
>>> 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
In contrast to timeit's fine level of granularity, the profile and pstats modules provide tools for identifying time critical sections in larger blocks of code.
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.
The doctest 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:
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
The unittest module is not as effortless as the doctest module, but it allows a more comprehensive set of tests to be maintained in a separate file:
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
Python has a ``batteries included'' philosophy. This is best seen through the sophisticated and robust capabilities of its larger packages. For example:
Note聽Two :about Tuples (distinguish between string type and聽 tuples tpye,especially聽zero or only one items contained in a tuples聽)
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:
>>> empty = ()
>>> singleton = 'hello',聽聽聽 # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
but if you write a statement like this :
>>> singleton = 'hello'聽聽聽聽聽聽聽聽聽 # it means that you define or construct a string type,not a tuples
>>>聽singleton
'hello'
聽
It is possible to nest lists (create lists containing other lists), for example:
>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[1][0]
2
>>> p[1].append('xtra')聽聽聽聽
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']
Note Two : about for statement
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:
valid statements: it will work normally
>>> for x in a[:]: # make a slice copy of the entire list
...聽聽聽聽聽聽 if len(x) > 6:聽
...聽聽聽聽聽聽聽聽聽聽聽a.insert(0, x)
...
>>> a
['defenestrate', 'cat', 'window', 'defenestrate']
invalid statement: you prompt will die
>>> for x in a :聽 # make a slice copy of the entire list
...聽聽聽 聽聽聽if len(x) > 6:
...聽聽聽聽聽聽聽聽聽 a.insert(0, x)
...
Note that: 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聽
Note Three: Compare for efficiency
str_test = []
#聽method one :
str_test.append('attach')
#method two:
str_test = str_test聽+ ['attach']
The聽Two聽method above has the聽same function ,but the more efficient one is the first one
聽
Note Four :about Defining Functions聽
Form 1:Default Argument Values
The default value is evaluated only once.
def f(a, L=[]):
聽聽聽 L.append(a)
聽聽聽 return L
print f(1)
print f(2)
print f(3)
This will print
[1] [1, 2] [1, 2, 3]
If you don't want the default to be shared between subsequent calls, you can write the function like this instead:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
Form 2 : Keyword Arguments
When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except
for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in
the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must
occur before **name) For example, if we define a function like this:
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
keys = keywords.keys()
keys.sort()
for kw in keys: print kw, ':', keywords[kw]It could be called like this:
cheeseshop('Limburger', "It's very runny, sir.",
"It's really very, VERY runny, sir.",
client='John Cleese',
shopkeeper='Michael Palin',
sketch='Cheese Shop Sketch')and of course it would print:
-- 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
Note that the sort() method of the list of keyword argument names is called before printing the contents of the keywords
dictionary; if this is not done, the order in which the arguments are printed is undefined.
-1 is actually an expression composed of the unary operator `-' and the literal 1<> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
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 />
聽聽