Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Asterisk in print function

Let's have a look:

print([object, ...], *, sep=' ', end='\n', file=sys.stdout)

http://docs.python.org/py3k/library/functions.html?highlight=print#print

How can we interpret that '*'? Usually an asterisk ('*') means numerous objects. But herein it is a mystery to me. Between two commas... I'm even afraid to think it may be a typo.

like image 691
Kifsif Avatar asked Sep 22 '12 09:09

Kifsif


People also ask

What does print (*) do in Python?

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

How do you print an asterisk in Python?

Print star or numberUse the print() function in each iteration of nested for loop to display the symbol or number of a pattern (like a star (asterisk * ) or number).

What does (*) mean in Python?

Practical Data Science using Python The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.

What does * mean in Python list?

Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times.


2 Answers

That's an error in the documentation, inserted by someone applying a new Python 3 feature to places where it shouldn't be used. It has since been fixed (see issue 15831).

The function signatures in the document used is given in a psuedo-formal-grammar form, but adding in the * marker only makes sense if you use actual python syntax. The [object, ...], * part of the signature should have been listed as *objects instead in that case.

The corrected version now reads:

print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False)

The online development version of the documentation has as of now not yet been updated, but the documentation source has been corrected; I'll see if we can request a regeneration of the docs.

To be clear: the * syntax is valid in Python 3 and means that the following arguments can only ever be used as keyword arguments, not positional arguments. This does however not apply to the print() function, as all positional arguments are to be printed anyway and could never be mistaken for the keyword arguments.

like image 98
Martijn Pieters Avatar answered Sep 29 '22 11:09

Martijn Pieters


It means that the following arguments are keyword-only i.e., you can't supply them as positional arguments, you must use their names e.g.:

>>> def f(*, a): pass
... 
>>> f(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes exactly 0 positional arguments (1 given)
>>> f(a=1)
>>> # ok

Another example:

>>> def g(*a, b): pass
... 
>>> g(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: g() needs keyword-only argument b
>>> g(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: g() needs keyword-only argument b
>>> g(1, b=2)
>>> # ok
>>> g(1, 2, b=3)
>>> # ok
like image 29
jfs Avatar answered Sep 29 '22 11:09

jfs