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
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:
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.
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 does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 6 years ago.
This is a basic question. Is there a difference in doing
def foo(*args, **kwargs):
"""standard function that accepts variable length."""
# do something
foo(v1...vn, nv1=nv1...nvn=nvn)
def foo(arg, kwargs):
"""convention, call with tuple and dict."""
# do something
mytuple = (v1, ..vn)
mydict = {nv1=nv1, ...nvn=nvn}
foo(mytuple, mydict)
I could do the same thing with both, except that the later has a weird convention of creating a tuple and dictionary. But basically is there a difference? I can solve the same computational problem of handling infinite things because dict and tuple can take care of that for me anyway?
Is this more of an idiomatic part of Python i.e a good Syntactic Sugar for things that you do anyway? I.e function is going to handle this for you!
PS: Not sure of so many downvotes though I agree this is a copy of Why use packed *args/**kwargs instead of passing list/dict? and probably it should be corrected in the duplicate information. And that question has recieved upvotes. So am I being downvotes for not being able to find that?
args and kwargs are just names.
What really matters here is the * and **.
In your second example you can only call the function with 2 arguments, against the first example where you can call the function with basically infinite arguments.