代码之家  ›  专栏  ›  技术社区  ›  Austin Adams

actionscript3的restParam的Python等价物

  •  3
  • Austin Adams  · 技术社区  · 16 年前

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

    1 回复  |  直到 16 年前
        1
  •  4
  •   Jochen Ritzel    16 年前
    def annihilateUnicorns(*unicorns):
        for i in unicorns: # stored in a list
            i.splode()
        return "404 Unicorns not found. They sploded."