在ActionScript3(Flash的编程语言,非常类似于Java——以至于它令人不安)中,如果我定义了一个函数,并希望用无止境的参数调用它,我可以这样做(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."

谢谢!:天

最佳答案

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

09-15 15:38