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.
Answers:
All arguments after the *
must be passed as keyword arguments. Those arguments cannot be passed as positionals.
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')
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.
All arguments after the *
must be passed as keyword arguments. Those arguments cannot be passed as positionals.
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')