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
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.
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:
Is there a way to pass optional parameters to a function?
(5 answers)
Closed 7 years ago.
I'm making a Script with a GUI Box in a CAD program, and the User selects about 7 different surfaces in the viewport. I then pass those values onto another Function when the User hits "OK"
The function that it is passed to looks like this
def MeshingTools(od_idSurf, trgSurf, PipeBodySurf, sealSurf, threadSurf, BodySurf, cplgEndSurf):
The problem is: if the user does not need to select one of those surfaces, I get a error saying, MeshingTools() takes exactly 7 non-keyword arguments (2 given)
How can I get around this issue?
UPDATE:
I tried keyword arguments and am not quite getting what I need.
def MeshingTools(**kwargs):
print kwargs
When I just select 1 surface, I get the following out
{'PipeBodySurf': (mdb.models['FullCAL4'].rootAssembly.instances['PinNew-1'].edges[151], mdb.models['FullCAL4'].rootAssembly.instances['PinNew-1'].edges[153])}
if I try to print PipeBodySurf , it says that global name is not defined.
Any ideas?
FINAL UPDATE (SOLVED)
Now I see that **kwargs creates a dictionary, so instead of using just the parameter name in the rest of the code, you have to use kwargs['parameter'] and then it will use the values
You can use arbitrary argument passing with * operation :
def MeshingTools(*args):
for i in args:
#do stuff with i
Functions can use special argument preceded with one or two * character to collect an arbitrary number of extra arguments. (* for positional arguments and ** for keyword arguments)
This question already has answers here:
Tkinter binding a function with arguments to a widget
(2 answers)
Closed 8 months ago.
I have a general question that I can't really find an answer to so hopefully you guys can help. I have a function that takes 3 parameters, below is an example of what I have.
def someFunction(self, event, string):
do stuff ..
self.canvas.bind("<Button-1>", self.someFunction("Hello"))
When I run this, I get an error saying that I passed someFunction 2 arguments instead of 3. I'm not sure why ..
Here you're binding the result of someFunction (or trying to anyway). This fails because when python tries to get the result of someFunction, it calls it only passing 1 argument ("Hello") when someFunction really expects 2 explicit arguments. You probably want something like:
self.canvas.bind('<Button-1>',lambda event: self.someFunction(event,"Hello"))
This binds a new function (which is created by lambda and wraps around self.someFunction) which passes the correct arguments.
Or,
def someFunction(self, string):
def fn(*arg)
print string
return fn
self.canvas.bind("<Button-1>",self.someFunction("Hello!"))