2010-10-03 46 views
3

在ActionScript 3中(Flash的編程語言,非常類似於Java--以至於它令人不安),如果我定義了一個函數並且希望它被無限參數調用,那麼我可以這樣做(restParam,我認爲這是所謂的):Python的等效ActionScript 3的restParam

function annihilateUnicorns(...unicorns):String { 
    for(var i:int = 0; i<unicorns.length; i++) { 
     unicorns[i].splode(); 
    } 
    return "404 Unicorns not found. They sploded."; 
} 

(那麼你可以把它與這個:) annihilateUnicorns(new Unicorn(), new Unicorn(), new Unicorn(), new Unicorn());

什麼是酷的是,所有這些額外的參數都存儲在一個數組。我將如何在Python中做到這一點?這顯然不起作用:

def annihilateUnicorns (...unicorns): 
    for i in unicorns : 
     i.splode() 
    return "404 Unicorns not found. They sploded." 

謝謝! :D

回答

4
def annihilateUnicorns(*unicorns): 
    for i in unicorns: # stored in a list 
     i.splode() 
    return "404 Unicorns not found. They sploded." 
+0

謝謝!這正是我想要的! – 2010-10-03 03:38:16