如何在python中使用变量作为func?

时间:2014-09-22 14:06:28

标签: python

def execute_as_vic(vic, cmd, a = None, b = None, c = None, d = None, e = None):
    if cmd in commands: #commands is a list
        if b == None and c == None and d == None and e == None and a != None:
            cmd(vic, a)
        if c == None and d == None and e == None and a != None and b != None:
            cmd(vic, a, b)
        if d == None and e == None and a != None and b != None and c != None:
            cmd(vic, a, b, c)
        if e == None and a != None and b != None and c != None and d != None:
            cmd(vic, a, b, c, d)
        if a != None and b != None and c != None and d != None and e != None:
            cmd(vic, a, b, c, d, e)

还有让它缩短的问题,我认为我以前见过的“args *”会起作用,但我不知道如何使用它:/有人可以解释一下吗?


我将它保留在同一个问题中,因为它仍然是相同的脚本:

def execute_command(connection, victim, cmd, a = None, b = None, c = None, d = None, e = None):

其中: 连接是执行执行命令的用户(讨厌那个复杂的狗屎)   - 受害者是我想执行该命令的用户   - cmd是执行的功能   - a,b,c,d,e是args。

!VICTIM GETS转入VIC! (这不是问题)

错误是:参数数量无效。

1 个答案:

答案 0 :(得分:2)

只需将函数作为参数传递而不调用它

def fun(some_arg):
    print some_arg * 2

def executecmd(cmd, arg):
    cmd(arg)

executecmd(fun, 21)

输出:

42

实际上没有其他内容,Python中的函数是first-class objects