相当于ActionScript 3的restParam的Python

2024-05-15 13:00:39 发布

您现在位置:Python中文网/ 问答频道 /正文

在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."

谢谢!:D个


Tags: newfor参数returnnotflashunicornthey
1条回答
网友
1楼 · 发布于 2024-05-15 13:00:39
def annihilateUnicorns(*unicorns):
    for i in unicorns: # stored in a list
        i.splode()
    return "404 Unicorns not found. They sploded."

相关问题 更多 >