##language:zh #pragma section-numbers on ## 默许导航,请保留 <> <> = Python快速介绍 = * 100820: [[http://py.kingsoft.net/s5/100820-introPy/|100820-introPy]] ~ ^珠海金山内部分享^ * 录音: [[http://py.kingsoft.net/media/record/100820_094M_zq-pyintro.MP3|100820_094M_zq-pyintro.MP3]] * 录像: [[http://www.youku.com/playlist_show/id_5066545.html|Pythonic4WPS - 专辑 - 优酷视频]] * 幻灯^s5^下载: [[http://py.kingsoft.net/s5/100820-introPy.7z|100820-introPy.7z]] * 070322: [[localshare:classes/classes2007/070322-introPy/|V0.9 Python快速介绍]] ~ ^07-03-22新浪内部第2次推广^ * 061009: [[localshare:classes/Classes2006/061009-pytut/|V0.8 Python介绍]] ~ [[BPUG/2006-12-02|CPUG第13次.中科院研究生院开源软件协会]] * 050904: [[localshare:classes/0904-tutpy/tutpython001/|V0.7 Python介绍]] ~ [[BPUG/2005-09-04|Python入门第一班--漫谈Python语言]] * 幻灯^s5^下载:[[localshare:classes/classes2007/070322-introPy.7z|070322-introPy.7z]] == 演示备案 == 备课,经典Pythonic 代码以支持展示 === iPython === * `dir()` * `help()` === 高级数据类型 === <> {{{ In [1]: li = range(10) In [2]: li Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [3]: li[6] Out[3]: 6 In [4]: li[:6] Out[4]: [0, 1, 2, 3, 4, 5] In [5]: li[6:] Out[5]: [6, 7, 8, 9] In [6]: li[:-6] Out[6]: [0, 1, 2, 3] In [7]: li[-6:] Out[7]: [4, 5, 6, 7, 8, 9] In [8]: li[::3] Out[8]: [0, 3, 6, 9] In [9]: li[:6:3] Out[9]: [0, 3] In [10]: li[6::3] Out[10]: [6, 9] In [11]: li[3:9:3] Out[11]: [3, 6] In [12]: li[3:9] Out[12]: [3, 4, 5, 6, 7, 8] }}} ==== 乘法表 ==== {{{ In [13]: for i in range(1,10,1): [ i*j for j in range(1,i) ] ....: Out[13]: [] Out[13]: [2] Out[13]: [3, 6] Out[13]: [4, 8, 12] Out[13]: [5, 10, 15, 20] Out[13]: [6, 12, 18, 24, 30] Out[13]: [7, 14, 21, 28, 35, 42] Out[13]: [8, 16, 24, 32, 40, 48, 56] Out[13]: [9, 18, 27, 36, 45, 54, 63, 72] }}} {{{ print "".join([('%s*%s=%s%s' % (y,x,x*y,'\n' if x==y else '\t')) for x in range(1,10) for y in range(1,10) if x >= y]) 输出: 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 }}} ==== 单行素数生成器! ==== * 展示思路... {{{ In [154]: a=7 In [156]: [d for d in range(2,a-1)] Out[156]: [2, 3, 4, 5] In [155]: [a%d for d in range(2,a-1)] Out[155]: [1, 1, 3, 2] In [162]: a=8 In [163]: 0 not in [a%d for d in range(2,a-1)] Out[163]: False In [167]: N=10 In [168]: [p for p in range(2,N) if 0 not in [p%d for d in range(2,p-1)]] Out[168]: [2, 3, 5, 7] }}} PyPrimeNumberGenerator: * <> === 函式参数 === {{{ 工厂模式... In [174]: def doit(do,i,j): .....: done = "%s %s %s" % (i,do,j) .....: print "want do::",done .....: return eval(done) In [125]: def myfun(*args): .....: '''try function def .....: ''' .....: print len(args),args .....: return args[0]+args[1] In [139]: def myfun(**kvar): .....: print kvar .....: for k in kvar: .....: kvar[k]+=2 .....: print kvar In [140]: myfun(k1=12,k2=23) {'k2': 23, 'k1': 12} {'k2': 25, 'k1': 14} }}} === OOP === * `重载运算符+` {{{ In [15]: class nplus: ....: def __init__(self,one): ....: self.one = one ....: def __add__(self,two): ....: return self.one*two In [16]: a=nplus(10) In [17]: a+2 Out[17]: 20 }}} ==== 自省! ==== {{{ In [19]: def reprself(item): ....: print "ID :",id(item) ....: print "TYPE :",type(item) ....: print "VALUE:",repr(item) ....: if hasattr(item,"__name__"): ....: print "NAME::",item.__name__ ....: if hasattr(item,"__calss__"): ....: print "CLASS::",item.__calss__.__name__ In [20]: reprself(reprself) ID : -1212717100 TYPE : VALUE: NAME:: reprself In [21]: reprself(123) ID : 136453144 TYPE : VALUE: 123 In [22]: reprself("123") ID : -1212647168 TYPE : VALUE: '123' In [23]: reprself([1,2,3]) ID : -1212664436 TYPE : VALUE: [1, 2, 3] In [24]: reprself((1,2,3)) ID : -1212700676 TYPE : VALUE: (1, 2, 3) In [25]: reprself({1:"1","2":2,3:[3,]}) ID : -1212705004 TYPE : VALUE: {3: [3], 1: '1', '2': 2} }}} * 追溯谁父辈: {{{#!python import traceback In [200]: def callme(): .....: s = traceback.extract_stack() .....: print "i'd call by %s !" % s[-2][2] In [201]: fun1() i'd call by fun1 ! }}} * 谁调用了俺? {{{ 参考下面的例子: [A.py] import B [C.py] import B [B.py] def who_import_me(): import sys frame = sys._getframe() importer_name = frame.f_back.f_back.f_code.co_filename return importer_name[:-3] print 'Hi module %s' % who_import_me() 运行结果: D:\Python Program\import>python A.py Hi module A D:\Python Program\import>python C.py Hi module C }}} ==== Mixin-混入! ==== {{{ In [202]: class A: .....: def __init__(self): .....: self.name = "class A" .....: def do(self): .....: print self.name In [203]: class B: .....: def __init__(self): .....: self.name = "class B" .....: def add(self,a,b): .....: return a+b In [212]: A.__bases__ +=(B,) In [213]: A. A.__doc__ A.__init__ A.__module__ A.add A.do }}} ==== Plugin ==== {{{ def p2(self,*arg):print "in %s !!!! %s"%(self.name,arg) setattr(A,"plugin",p2) }}} === 对象序列化 === {{{ In [234]: lis=[1,2,"as",[1,2],(2,3,3),{}] In [235]: pickle.dump(lis,open("lisA.dump","w")) In [237]: li2 = pickle.load(open("lisA.dump")) In [238]: li2 Out[238]: [1, 2, 'as', [1, 2], (2, 3, 3), {}] }}} === 编译字节文件 === {{{ def myfunc(alist): return len(alist) the following command can be used to get the disassembly of myfunc(): import dis >>> dis.dis(myfunc) 2 0 LOAD_GLOBAL 0 (len) 3 LOAD_FAST 0 (alist) 6 CALL_FUNCTION 1 9 RETURN_VALUE 10 LOAD_CONST 0 (None) 13 RETURN_VALUE }}} == 函式编程 == {{{ map(func, list, …) 相当于[func(l) for l in list] In [241]: map((lambda x:x*x),range(10)) Out[241]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] In [254]: map((lambda x,y:x+y),range(0,10,1),range(10,0,-1)) Out[254]: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] In [257]: a=range(1,10,2) In [258]: a Out[258]: [1, 3, 5, 7, 9] In [259]: b=range(10,0,-2) In [260]: b Out[260]: [10, 8, 6, 4, 2] In [264]: zip(a,b) Out[264]: [(1, 10), (3, 8), (5, 6), (7, 4), (9, 2)] In [101]: [(lambda x,y : x+y)(i,j) for i,j in zip(a,b)] Out[101]: [11, 11, 11, 11, 11] In [102]: ["%s-%s "%(i,j) for i,j in zip(a,b)] Out[102]: ['1-10 ', '3-8 ', '5-6 ', '7-4 ', '9-2 '] }}} === 中蟒 === <> = 反馈 = ::-- ZoomQuiet [<>]