gpt4 book ai didi

python - 如何像 JavaScript 一样在 Python 中包装函数?

转载 作者:行者123 更新时间:2023-12-01 08:57:33 26 4
gpt4 key购买 nike

我想创建一个执行打印的新函数,前提是Debug=True。这就是我在 JavaScript 中的做法:

function log(...args) {
if (debug) console.log.apply(null,args)
}

 function log() {
if (debug) console.log.apply(null,arguments)
}

如何在 Python 中做类似的事情?

问题:如何传递我在 Python 中获得的所有参数?

最佳答案

您可以使用可变参数和可变关键字参数:

# assuming debug is a global variable initialized as either True or False
def log(*args, **kwargs):
if debug:
print(*args, **kwargs)

关于python - 如何像 JavaScript 一样在 Python 中包装函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52695394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com