2016-02-25 121 views
5

在Ruby中,yield關鍵字用於產生執行塊的閉包。Python收益率與Ruby收益率

此關鍵字在Python語言中有何不同?

+3

我會回答,但我想想[這](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python)涵蓋了它 – Alex

回答

8

在ruby中,yield是一個用於調用匿名函數的快捷方式。 Ruby具有將匿名函數傳遞給方法的特殊語法;該語法被稱爲block。由於該功能還沒有名字,您使用的名稱產生調用的函數:

def do_stuff(val) 
    puts "Started executing do_stuff" 
    yield(val+3) 
    yield(val+4) 
    puts "Finshed executing do_stuff" 
end 

do_stuff(10) {|x| puts x+3} #<= This is a block, which is an anonymous function 
          #that is passed as an additional argument to the 
          #method do_stuff 

--output:-- 
Started executing do_stuff 
16 
17 
Finshed executing do_stuff 

在蟒蛇,當你看到一個函數的定義,這意味着該函數是generator內部收益率。生成器是一種特殊類型的函數,可以在執行中停止並重新啓動。這裏有一個例子:

def do_stuff(val): 
    print("Started execution of do_stuff()") 

    yield val + 3 
    print("Line after 'yield val + 3'") 
    yield val + 4 
    print("Line after 'yield val + 4'") 

    print("Finished executing do_stuff()") 


my_gen = do_stuff(10) 

val = next(my_gen)  
print("--received {} from generator".format(val)) 

輸出:

Started execution of do_stuff() 
--received 13 from generator 

更多代碼:

val = next(my_gen)  
print("--received {} from generator".format(val)) 

輸出:

從輸出中,你可以看到,yield導致結果被遣返d;那麼執行立即停止。當你再次在生成器上調用next()時,將繼續執行,直到遇到下一個yield語句,該語句返回一個值,然後執行再次停止。

+3

基本上,Python的'yield'關鍵字等同於Ruby的'Enumerator: :Yielder#yield'。 –

+0

謝謝!我不知道Enumerator :: Yielder類! – noname

+0

關於'Enumerator :: Yielder'和'Enumerator :: Generator'的更多細節我推薦:http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-可待懶 –

1

在Ruby中,收益率用於反彈控制以阻止(如匿名函數)執行塊的語句,然後彈回到塊的調用位置。

隨着yield args你可以傳遞參數來將擋,並與lvar = yield 你可以得到什麼回來了,它控制退出塊之後綁定到LVAR。這是Ruby中一個非常普遍且一致的功能設計。當然, 你可以應用這個想法迭代集合。

而在Python中,大部分人使用產量,以方便項目在一定程度上收集有效訪問,他們專注於迭代一次,並實時生成,一旦被稱爲的想法,這是主要的使用產量的在Python中。

僅供參考,至少在使用它的方式上,Python和Ruby之間的差異性不大,收益率爲。 (顯然它們的實現方式不同,至於python,yield創建了一個生成器,除非迭代開始,否則它將不會運行任何代碼)。例如,在Python中使用產生的方式在Ruby中與上下文管理器完全相同。

from contextlib import contextmanager 
@contextmanager 
def openfile(name, mode): 
    f= open(name, mode) 
    yield f 
    f.close() 

with openfile('log.txt', 'r') as handle: 
    for line in handle: 
     print line 

這裏,產量文件句柄,並執行與語句恰好一次,然後反彈至文件關閉聲明