Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when passing unpacked argument to print in Python

Instead of a simple debug/log print as this:

print "error ", error_number

I would like to use a log function that I can expand when required looking something like this:

def log(condition, *message):
    if(<do something here...>):
        print(*message)
        <perhaps do something more...>

and call it like this:

log(condition, "error ", error_number)

But I get the following syntax error:

print *message
      ^ SyntaxError: invalid syntax

Is it a limitation of the print function or is there some way to make it work? If not, is there an equivalent to print that I could use?

I'm using Python 2.7 by the way...

like image 555
hobb Avatar asked Nov 09 '11 07:11

hobb


1 Answers

print is not a function in Python 2.x. In the first snippet you are printing a tuple and the last one has invalid syntax. If you want to use the print function, you need to enable it via from __future__ import print_function.

like image 113
wRAR Avatar answered Sep 25 '22 21:09

wRAR