【问题标题】:Bash has $@ and $*. Does python have an equivilant? [duplicate]Bash 有 $@ 和 $*。 python有等价物吗? [复制]
【发布时间】:2020-06-16 18:11:50
【问题描述】:

在 bash 中,您可以使用 $@ 或 $* 将函数/方法的参数列表作为列表获取。 python有类似的东西吗?我正在尝试进行一种日志记录,其中函数的日志将打印出函数参数。

例子

def foo(a: str, b: List) 
    print(magic_get_args_function());
    ....

foo("h", [1, 2, 3])

输出

h, [1, 2, 3]    # or even include the argument names if possible

【问题讨论】:

  • 旁白:$* 不返回参数作为列表,正如这个问题的第一句话所声称的那样;它将它们强制为单个字符串(然后,如果不加引号使用,则将其拆分回列表并进行全局扩展,但不能保证与 original 参数列表相同)。始终使用"$@"(带引号)。
  • @CharlesDuffy 感谢您的澄清

标签: python


【解决方案1】:

这个怎么样?

from inspect import getcallargs

def foo(a: str, b: list):
    print(getcallargs(foo, a, b))

foo("h", [1, 2, 3])
{'a': 'h', 'b': [1, 2, 3]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2012-06-08
    • 2021-05-14
    • 2010-11-27
    相关资源
    最近更新 更多