This question already has answers here:
Positional argument vs keyword argument
(9 answers)
Closed 6 months ago.
I found * , / in the typing source code, but I don't understand what it means.
def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
pass
Anything before / means that the argument is position-only (see PEP-570, while anything after *is keyword only (see PEP-3102). Anything in between can be both positional and keyword.
Related
This question already has an answer here:
Keyword only parameter [duplicate]
(1 answer)
Closed 3 months ago.
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.
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')
This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
Closed 2 years ago.
The Python code below throws an error.
def f(a,*, **c):
pass
The error says that SyntaxError: named arguments must follow bare *. I couldn't understand what this means in this case. I have specified a parameter after the bare *, yet I get an error.
A standalone * parameter is only necessary (and as you've seen, allowed) if you specify one or more named keyword-only parameters following it. Since you have no named keyword-only parameters, you need to omit it.
def f(a, **c):
pass
If you want a to be a positional-only argument, you need to use a standalone / parameter:
def f(a, /, **c):
pass
This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
Closed 2 years ago.
What is the meaning of a lone star '*' in the parameter list of a Python function?
I found it in the source of scikit-learn and haven't seen it before. I'm familiar with the concepts of positional and keyword arguments (*args, **vargs). I'm assuming, here it has something to do with the _deprecate_positional_args decorator, but the syntax of a lone star as a function parameter seems to be allowed in pure Python 3.7 even without the decorator.
My guess is, it makes it impossible to specify any keyword arguments after the star as positional arguments (as would actually make sense for a parameter called 'safe').
# Part of https://github.com/scikit-learn/scikit-learn.git
# commit 7117a6313791db6f8b737bac28c1f47277a68cfb
# Quoting from sklearn/base.py:
# ...
from .utils.validation import _deprecate_positional_args
# ...
#_deprecate_positional_args
def clone(estimator, *, safe=True):
"""Constructs a new estimator with the same parameters.
(rest omitted)
"""
# ...
My guess is, it makes it impossible to specify any keyword arguments after the star as positional arguments (as would actually make sense for a parameter called 'safe').
You are right, arguments following lone * are dubbed keyword-only arguments, this feature is defined by PEP 3102.
This question already has answers here:
What is the meaning of a forward slash "/" in a Python method signature, as shown by help(foo)? [duplicate]
(1 answer)
What does the slash mean when help() is listing method signatures?
(3 answers)
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 4 years ago.
When using help function on some method, I get a short text describing that method. In the first line, most of the time it says:
"method name" (self, iterable, /).
Can anyone tell me what does self and / mean for that method?
This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
Closed 4 years ago.
I recently worked on several projects and see a new syntax code, a method that get a * arguments(i know about *args and **kwargs)
for an example in django 2.0.4:
class DataListView(ListView):
...
def get_context_data(self, *, object_list=None, **kwargs): # * argument without suffix
return super().get_context_data(object_list, **kwargs)
and Question: what is meaning of * arguments in python function/method?
It's so that object_list must be a named argument. All positional arguments will be captured by * and ignored.
This was added in python 3.0 and is described in PEP 3132 -- Extended Iterable Unpacking