ѡ ֻ | ʵ԰
 һ
 ע
̳ :::. :::. PHP

© zzf342100 ʿ   /  2014-5-20 09:16  /   0 ղ Ϣ ֹҵʹãվԶ֣

ΪʲôҪѧϰPython


ҲPythonһЩõĿ߿ܣֻԱС̸ֵ֧ԱԵķչԽԽӽLispҲΰԭ



ԣѧϰPython̸̸꣬ҶPythonһС⣬ҲһЩ̸̸Լ΢뷨ҲΪʲôѧϰPython
ҲPythonһЩõĿ߿ܣֻԱС̸ֵ֧Ա
ԵķչԽԽӽLispҲΰԭ
һѧϰPythonԭ
һ.̷ʽ
PythonһŶ෶ʽıԣνĹʽͺʽĽϡ
󲿷˽ӴԶǴӹʽʼģԭΪʽijʽзʽͳһģָйͳһ
͵CҲǴCʼѧϰģʽijƱдΪ򵥣Ƿ˻˼ʽ
PythonȻһԣ ոҲԿһ󣬵Pythonʤιʽûġ
粻Ҫʹľ̬
1 def a_plus_b(a,b):  
2 return a+b
pythonƵʱ䵱һķʽдҲ˵ƴһЩȣpythonĶ̬һDuck typingѼͣ
Ѽͣ˵ΪһĶӾᡰ¸¡УҾͿ԰Ѽӡ
3 def use_duck( Duck ):  
4 Duck.swim()  
5 Duck.gaga()  
6 class Duck:  
7 def swim(self):  
8 ...  
9 def gaga(self):  
10 ...
ʹ
11 little_duck Duck()  
12 use_duck( little_duck )
Duck࣬Ըȡκε֣߼̳ȡһֻ֣Ҫʵ swim() gaga() Ϳ԰Ѽ
Ѽͣܶ˲ΪʲôҪṩһӿ涨ѼӵΪҼȲ֧ҲԣҵĹ۵ģ
1.ڲļ飬϶̬Ե
2.ṩ˽ӿڹ淶ǾͲѼˣֱӽж̬
pythonֵ֧ĺʽ
lambda
ʽ̵ĶǽDZһĴڳ򵥵ʲô
1.Ըֵ
2.Ϊ
3.ԸıֵErlang⣩
4.Ҳ˵˺
㱳̺żɼԵ֪ʶlambdaҲͼģͣͣһ񶨴𰸡һ
lambda 㣬ʲô
13 map(lambda n:2*n,[1,2,3,4,5])
1.lambda n:2*n Ϊһ
2.lambda Ϊһ map() ,Ҳ˵ҵĸ߽׺ԽһΪݣҲΪܵĸߵȴ
ڸֵ͸ıֵַʽ
1.f = fun() ı亯״ֻ̬ıƣ˵ǿԸֵ
2.ʹñհΪı亯״̬ʽʹװɺ״̬ı
ʽ̵ʹҲ߳ĿɶԺͼٴ룬ܹıﺯĹܣMapReduceԺʽ̵˼
Map(func,List)
ǽfunc ListеÿһԪ
ԸղŵӾ
14 map(lambda n:2*n,[1,2,3,4,5])
˺أ[2,4,6,8,10]
Ҫ֪ķʽǵƷʽ
Ȼʽ̲ô仰˵ģ⺯ʽ̵ĺ
.һЩ˼
Լ㣺
һ쳲python ô
15 >>> def fib():  
16     a , b = 0 ,1
17     while 1:  
18         yield b  
19         a , b = b ,a+b         
20 >>> f = fib()
ʵyield һɵÿε
f.next()ͿԲһ쳲ֵڲ״̬ɵ洢
ڷһɵҪȷλʹ itertools.islice
Э̣
ЭҲһyieldĸҪģʽ΢̵߳Эʽģʽ
21 def coroutine(func):  
22      def ret():  
23          f = func()  
24          f.next()  
25          return f  
26      return ret  
27 @coroutine
28 def consumer():  
29      print "Wait to getting a task"
30      while 1:  
31          n = (yield)  
32          print "Got %s",n   
33 import time  
34 def producer():  
35      c = consumer()  
36      while 1:  
37          time.sleep(1)  
38          print "Send a task to consumer"
39          c.send("task")   
40 if __name__ == "__main__":  
41      producer()
Э̴ĺôǿֱӵ̣߳ҲΪʲôЭ̶̵߳ԭ߳ռʽЭЭʽ
.̬Դĺô
ӳƴĿ(ֻȰⷽ˲еĸо)˵̬ԣpythonԼ˸ʱŮѻţϹ
ȻΪʱٿ˵ѼߣҲǡڿ뻭ҡܵģٿҪȻҪⷽ
̬ԵCPUܼȻȲC/C++
̣֮Python
ԭӣhttp://www.cnblogs.com/Bozh/archive/2012/06/18/2553224.html
ղ
ظ
Աֵ𾴵ģԱ˫ħʦ˫֣ǰѿζĴ˷ḻʵ

1 ظ

ʦ 2014-5-20 19:35:44
| ʾȫ¥
ɳ
л
ظ ʹõ ٱ