What does singular "*" as an argument in a python function definition do?

Question:

I am trying to look through some code and don’t know what the asterisk in the following code means.

def pylog(func=None, *, mode='cgen', path=WORKSPACE, backend='vhls', 
          board='ultra96', freq=None):

What does the lonely asterisk signify in a function definition when not followed by the name of an argument?

I can only find results for *foo.

Asked By: wkoz

||

Answers:

All arguments after the * must be passed as keyword arguments. Those arguments cannot be passed as positionals.

Answered By: jkr

This syntax forces arguments after the * to be called with their keyword names when someone calls the function/method.

Example:

# This is allowed
pylog(math.log, mode='cgen')

# This is *NOT* allowed
pylog(math.log, 'cgen')
Answered By: Mike L
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.