I'm trying to use a list as arguments, using the :
>>> l = [1,2,3]
>>> print( *l )
I got an error :
File "<stdin>", line 1
t*
^
SyntaxError: invalid syntax
I'm using python 2.7 :
>>> import sys
>>> print sys.version
2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2]
What am I missing ? Thank you ! :)
By default, print
isn't a function in Python 2.7. To use the function instead of the statement in a given module, use a future statement:
from __future__ import print_function
This needs to go at the top of your file, before any code that isn't a future statement (or the module docstring), because the compiler needs to see future statements first to compile the rest of the module differently based on the future statement.
print
is NOT a function in Python 2.7. It is a statement. So, you should do
print l # [1, 2, 3]
If you want to use print
as a function in Python 2.7, you should import print_function
from __future__
, like this
from __future__ import print_function
l = [1,2,3]
print(l) # [1, 2, 3]
print(*l) # 1 2 3
如果要使用print
作为函数,则必须使用__future__
或将python升级到3+
Is this what you are searching for?
>>> l = [1, 2, 3]
>>> def x(*args):
... print args[0]
... print args
>>> x(*l)
1
(1, 2, 3)
If yes, take also a look at Arbitrary Argument Lists in the Python documenation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.