将解压缩参数传递给Python时打印时出现语法错误

hob*_*obb 5 python argument-unpacking python-2.7

而不是像这样简单的调试/日志打印:

print "error ", error_number
Run Code Online (Sandbox Code Playgroud)

我想使用一个日志功能,我可以在需要时展开这样的东西:

def log(condition, *message):
    if(<do something here...>):
        print(*message)
        <perhaps do something more...>
Run Code Online (Sandbox Code Playgroud)

并称之为:

log(condition, "error ", error_number)
Run Code Online (Sandbox Code Playgroud)

但是我得到以下语法错误:

print *message
      ^ SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

它是打印功能的限制还是有某种方法使它工作?如果没有,是否有相当于我可以使用的印刷品?

我顺便使用Python 2.7 ...

wRA*_*RAR 9

print不是Python 2.x中的函数.在第一个片段中,您正在打印元组,而最后一个片段的语法无效.如果要使用打印功能,则需要通过它启用它from __future__ import print_function.


Oli*_*ver 7

如果您不想使用__future__,可以像这样定义日志记录功能:

def log(condition, *message):
    if(<do something here...>):
        print ' '.join(str(a) for a in message)
        <perhaps do something more...>
Run Code Online (Sandbox Code Playgroud)