I have the following code:
def foo(func, *args, named_arg = None):
return func(*args)
returning a SyntaxError:
File "tester3.py", line 3
def foo(func, *args, named_arg = None):
^
Why is that? And is it possible to define somewhat a function in that way, which takes one argument (func), then a list of variable arguments args before named arguments? If not, what are my possibilities?
The catch-all *args parameter must come after any explicit arguments:
def foo(func, named_arg=None, *args):
If you also add the catch-all **kw keywords parameter to a definition, then that has to come after the *args parameter:
def foo(func, named_arg=None, *args, **kw):
Mixing explicit keyword arguments and the catch-all *args argument does lead to unexpected behaviour; you cannot both use arbitrary positional arguments and explicitly name the keyword arguments you listed at the same time.
Any extra positionals beyond func are first used for named_arg which can also act as a positional argument:
>>> def foo(func, named_arg = None, *args):
... print func, named_arg, args
...
>>> foo(1, 2)
1 2 ()
>>> foo(1, named_arg=2)
1 2 ()
>>> foo(1, 3, named_arg=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() got multiple values for keyword argument 'named_arg'
>>> foo(1, 2, 3)
1 2 (3,)
This is because the any second positional argument to foo() will always be used for named_arg.
In Python 3, the *args parameter can be placed before the keyword arguments, but that has a new meaning. Normally, keyword parameters can be specified in the call signature as positional arguments (e.g. call your function as foo(somefunc, 'argument') would have 'argument' assigned to named_arg). By placing *args or a plain * in between the positional and the named arguments you exclude the named arguments from being used as positionals; calling foo(somefunc, 'argument') would raise an exception instead.
No, Python 2 does not allow this syntax.
Your options are:
1) move the named arg to appear before *args:
def foo(func, named_arg = None, *args):
...
2) use **kwargs:
def foo(func, *args, **kwagrs):
# extract named_arg from kwargs
...
3) upgrade to Python 3.
change the order of the arguments to this:
def foo(func, named_arg = None, *args):
return func(*args)
According to the docs, no:
Tutorial section 4.7.2 says:
In a function call, keyword arguments must follow positional
arguments.
...and 4.7.3 says about 'Arbitrary argument lists':
Before the variable number of arguments, zero or more normal
arguments may occur.
...so if you're going to use all three argument types, they need to be in the sequence
Positional args
Named args
variable/arbitrary args
Related
I was going trough some code, and I came across the following function:
def foo(self, arg1=1, *, arg2=2):
pass
I was surprised to see keyword arguments on the left side of the *, the positional arguments. I notice then that I can call foo in both of the following ways:
>>> foo(1)
>>> foo(arg1=1)
I think I would be expecting the second call to fail as I am calling foo using a named argument by providing the keyword arg1.
With that said, am I using positional arguments in both scenarios, or is the second call to foo a named argument?
The best sentence that I found that best describes this is:
"The trick here is to realize that a “keyword argument” is a concept of the call site, not the declaration. But a “keyword only argument” is a concept of the declaration, not the call site."
Below is a concise explanation copied from here in case the link dies at some point.
def bar(a, # <- this parameter is a normal python parameter
b=1, # <- this is a parameter with a default value
*, # <- all parameters after this are keyword only
c=2, # <- keyword only argument with default value
d): # <- keyword only argument without default value
pass
The arg1 argument is allowed to be called as either a positional argument, or a keyword argument.
As of Python 3.8, it is possible to specify some arguments as positional only. See PEP 570. Prior to 3.8, this isn't possible unless you write a python C extension.
The 3.8 syntax looks like this (directly from the PEP):
def name(positional_only_parameters, /, positional_or_keyword_parameters,
*, keyword_only_parameters): ...
...prior to 3.8, the only legal syntax is this:
def name(positional_or_keyword_parameters, *, keyword_only_parameters): ...
Despite the similarity in syntax, there is no relationship between keyword arguments and parameters with default values.
key=value is used both to assign a default value to a parameter when the function is defined, and to pass an argument for a parameter when the function is called.
A parameter without a default value can be assigned using a keyword argument:
def foo(a):
return a + 3
assert foo(a=5) == 8
A parameter with a default value can be assigned without a keyword argument:
def foo(a=5):
return a + 3
assert foo() == 8
assert foo(1) == 4
Is a complex answer by using the * like the argument ( the Python version, define and use it ).
In your case:
>>> foo(1,2,)
>>> foo(1,2)
Note that:
Try to use this and see the difference ( without self ):
>>> def foo( arg1=1, *, arg2=2):
... pass
...
>>> foo(arg1=1,arg2=2)
>>> def foo( arg1=1, *, arg2=2):
... pass
...
>>> foo(arg1=1,arg2=2)
>>> foo(arg1=1)
>>> foo(arg1=1,)
>>> foo(arg2=2)
>>> foo(arg2=2,1)
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> foo(2,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes from 0 to 1 positional arguments but 2 were given
>>> foo(arg1=1,2)
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
One star * defines positional arguments (you can receive any number of arguments and treat the passed arguments as a tuple);
Two stars ** define keywords arguments.
A good example comes from this tutorial (the keyword-only arguments without positional arguments part):
def with_previous(iterable, *, fillvalue=None):
"""Yield each iterable item along with the item before it."""
previous = fillvalue
for item in iterable:
yield previous, item
previous = item
>>> list(with_previous([2, 1, 3], fillvalue=0))
[(0, 2), (2, 1), (1, 3)]
That its value could be sent as positional arguments.
See this tutorial for more info about arguments.
Python 3.x introduces more intuitive keyword-only arguments with PEP-3102 (you can specify * in the argument list).
One good alternative answer is this:
I defined a function like this:
def func(must, op1=1, op2=2, op3=3, *arguments, **keywords):
print(must, op1, op2, op3)
for arg in arguments:
print(arg)
for kw in keywords.keys():
print(kw, ":", keywords[kw])
I called it:
func(1, op1=1, op2=2, op3=3, 1, 3)
Of course, I got an error:
Error information
In my mind, *arguments are positional parameter. And the op1, op2, op2 are keyword parameters. So *arguments should be before op1, op2, op3. Then I changed the definition:
def func(must, *arguments, op1=1, op2=2, op3=3, **keywords):
print(must, op1, op2, op3)
for arg in arguments:
print (arg)
for kw in keywords.keys():
print (kw, ":", keywords[kw])
Why doesn't the Python interpreter think the first definition is a mistake?
Hi All,
I updated this topic.
After discuss with my colleague, I think that:
definition of a function, the parameters are default or non-default parameters
call a function, the parameters are positional or keyword parameters
So, whether the parameter is a positional parameter depends on you call the function, not define the function.
While slightly counterintuitive, the first definition is completely unambiguous:
func(must, op1=1, op2=2, op3=3, *args, **kwargs)
This means that op1, op2, and op3 are optional positional arguments. You can't specify any of *args without explicitly setting all three first. You got the error because you attempted to place positional arguments after keywords. It's irrelevant that the keywords name positional arguments. You can make the same call like this:
func(1, 1, 2, 3, 1, 3)
That will not raise an error. Neither will
func(1, 2)
In that case, only op1 will have a non-default value, *args will be empty, and you can set whatever keywords you want, including op2 and op3.
Aside from *args, all positional arguments are named and can be passed in by keyword as well. In practice, though, all arguments that come after the first one passed in as a keyword must be passed in as a keyword. It therefore follows that you can't pass in *args if you pass in any named positional argument as a keyword.
In your second example, you've made the op arguments keyword-only by placing them after the splat (*). Any positional arguments after the first are therefore absorbed into *args.
Keep in mind that keyword-only arguments don't need defaults. A function like def func(*, a): pass can only be invoked as func(a=3) and never as func(3).
On a side note, the splat argument is conventionally named *args and the splatty splat one **kwargs. I have stuck with that convention throughout my answer.
At the first example, you can call your function like this (it's still working):
func(9, 9, 9, 9)
Python determine which parameter is keyword when you call a function.
Here's more example:
def func(a,b,c=1,d): # -> a, b, c, d are parameters and c has default value = 1
pass
func(1, 2, 3, 4) # -> call with 4 postional parameters
func(1, 3, d=3) # -> call with 2 positional parameters and 1 keyword parameters
What does a bare asterisk in the parameters of a function do?
When I looked at the pickle module, I see this:
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
I know about a single and double asterisks preceding parameters (for variable number of parameters), but this precedes nothing. And I'm pretty sure this has nothing to do with pickle. That's probably just an example of this happening. I only learned its name when I sent this to the interpreter:
>>> def func(*):
... pass
...
File "<stdin>", line 1
SyntaxError: named arguments must follow bare *
If it matters, I'm on python 3.3.0.
Bare * is used to force the caller to use named arguments - so you cannot define a function with * as an argument when you have no following keyword arguments.
See this answer or Python 3 documentation for more details.
While the original answer answers the question completely, just adding a bit of related information. The behaviour for the single asterisk derives from PEP-3102. Quoting the related section:
The second syntactical change is to allow the argument name to
be omitted for a varargs argument. The meaning of this is to
allow for keyword-only arguments for functions that would not
otherwise take a varargs argument:
def compare(a, b, *, key=None):
...
In simple english, it means that to pass the value for key, you will need to explicitly pass it as key="value".
def func(*, a, b):
print(a)
print(b)
func("gg") # TypeError: func() takes 0 positional arguments but 1 was given
func(a="gg") # TypeError: func() missing 1 required keyword-only argument: 'b'
func(a="aa", b="bb", c="cc") # TypeError: func() got an unexpected keyword argument 'c'
func(a="aa", b="bb", "cc") # SyntaxError: positional argument follows keyword argument
func(a="aa", b="bb") # aa, bb
the above example with **kwargs
def func(*, a, b, **kwargs):
print(a)
print(b)
print(kwargs)
func(a="aa",b="bb", c="cc") # aa, bb, {'c': 'cc'}
Semantically, it means the arguments following it are keyword-only, so you will get an error if you try to provide an argument without specifying its name. For example:
>>> def f(a, *, b):
... return a + b
...
>>> f(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() takes 1 positional argument but 2 were given
>>> f(1, b=2)
3
Pragmatically, it means you have to call the function with a keyword argument. It's usually done when it would be hard to understand the purpose of the argument without the hint given by the argument's name.
Compare e.g. sorted(nums, reverse=True) vs. if you wrote sorted(nums, True). The latter would be much less readable, so the Python developers chose to make you to write it the former way.
Suppose you have function:
def sum(a,key=5):
return a + key
You can call this function in 2 ways:
sum(1,2) or sum(1,key=2)
Suppose you want function sum to be called only using keyword arguments.
You add * to the function parameter list to mark the end of positional arguments.
So function defined as:
def sum(a,*,key=5):
return a + key
may be called only using sum(1,key=2)
I've found the following link to be very helpful explaining *, *args and **kwargs:
https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
Essentially, in addition to the answers above, I've learned from the site above (credit: https://pythontips.com/author/yasoob008/) the following:
With the demonstration function defined first below, there are two examples, one with *args and one with **kwargs
def test_args_kwargs(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
# first with *args
>>> args = ("two", 3,5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5
# now with **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
So *args allows you to dynamically build a list of arguments that will be taken in the order in which they are fed, whereas **kwargs can enable the passing of NAMED arguments, and can be processed by NAME accordingly (irrespective of the order in which they are fed).
The site continues, noting that the correct ordering of arguments should be:
some_func(fargs,*args,**kwargs)
I'm a few months into learning python.
After going through pyramid tutorials I'm having trouble understanding a line in the init.py
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import (
DBSession,
Base,
)
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
return config.make_wsgi_app()
I'm lost about settings=settings in the configurator argument.
What is this telling python?
Python functions support keyword arguments:
def add(a, b):
return a + b
add(a=1, b=2)
This happens here.
Configurator(settings=settings)
The first settings is the name of the parameter in the __init__ of Configurator. The second is a name for an object in the current name space.
Python supports calling any callable object (i.e. functions, constructors, or even objects understanding __call__ method) specifying positional arguments, named arguments, or even both types of arguments.
When you pass named arguments, they must be after the positional arguments (if any is passed).
So you can call any function, like:
def f(a, b):
return a + b
In the following ways:
f(1, 2)
f(1, b=2)
f(a=1, b=2)
f(b=1, a=2) # Order doesn't matter among named arguments
While the following forms will trigger an error:
f(a=1, 2) # Named arguments must appear AFTER positional arguments
f(1, a=2) # You are passing the same argument twice: one by position, one by name
So, when you pass a named parameter, be sure you don't pass the same parameter twice (i.e. also by position), and also check that the argument name exists (also: if it is documented that the argument could/should be passed by name, respect their names on inheritance/override).
Additionally Python supports passing *arguments and **keyword_arguments. These are additional arguments you can process in a variadic way, as many languages support them.
*args (the name doesn't matter - it must have an asterisk to be a positional variadic; it's a tuple) holds remaining unmatched positional arguments (instead of getting a TypeError for positional argument being unexpected, such argument gets into *args as an element).
**kwargs (the name doesn't matter - it must have two asterisks to be a named/keyword variadic; it's a dictionary) holds the remaining unmatched named arguments (instead of getting a TypeError for named argument being unexpected, such argument gets into **kwargs as an element).
So, perhaps you see a function like this:
def f(*args, **kwargs):
...
You can call it with any parameters you want:
f(1, 2, 3, a=4, b=5, c=6)
Just keep the order: named arguments come after positional arguments.
You can declare a function like this:
f(m1, m2, ..., o1=1, o2=2, ..., *args, **kwargs):
pass
Understanding the following:
m1, m2, ... are mandatory: when you call, you must fill them either positionally or respecting their name.
o1, o2, ... are optional: when you call, you can omit such parameters (omitting them implies not passing them by position nor by name), and they will hold the value after the equal sign (such value is evaluated when the function is declared - avoid using mutable objects as their values).
args and kwargs are what I explained you before: Any unmatched argument by position and by name go into these parameters. With these features, you will have a clear distinction between what is a parameter and what is an argument.
All these parameters are optional. You can choose not to use mandatory and only optionals. You can choose not to use *args but yes **kwargs, and so. But respect the order in the declaration: mandatory, optional, *args, **kwargs.
When you call the method and pass the arguments the semantic is very different so be wary:
f(1) # passes a positional argument. Has nothing to do with the parameter being mandatory.
f(a=1) # passes a named argument. Has nothing to do with the parameter being optional.
f(**i) # UNPACKS the positional arguments. Has nothing to do with the function having a *args parameter, but *args will hold any unpacked -but unmatched- positional argument from i (which is any type of sequence or generator)
f(**d) # UNPACKS its values as named arguments. Has nothing to do with the function having a **kwargs parameter, but **kwargs will hold any unpacked -but unmatched- argument from d (which is a dict having string keys).
When you make the call, you can pass them as you like, which has nothing to do with the actual method signature (i.e. expected parameters), but respect the order as with parameters: positional, named, *positionalUnpack, **keywordUnpack, or you will get a nice TypeError.
Examples:
def f(a, b=1, *args, **kwargs):
pass
Valid calls:
f(1) # a = 1, b = 2, args = (), kwargs = {}
f(*[1]) #a = 1, b = 2, args = (), kwargs = {}
f(*[3, 4]) #a = 3, b = 4, args = (), kwargs = {}
f(**{'a':1, 'b':3}) #a = 1, b=3, args = (), kwargs = {}
f(1, *[2, 3, 4], **{'c': 5}) #a = 1, b=2, args=(3, 4), kwargs = {'c': 5}
Again beware:
Do not pass the same parameter twice (a clash would occur between **unpacked arguments with other named arguments, or positional arguments, or *unpacked arguments).
If you want to pass *args or **kwargs to a super call, ensure using the unpack syntax:
def my_method(self, a, b, *args, **kwargs):
super(MyClass, self).my_method(a+1, b+1, *args, **kwargs)
It's saying that it is passing the local name settings as the argument named settings in the Configurator.
For a function or constructor call of the form x=y, the x is the argument name used locally on the function/constructor side, while y is the name on the caller side. They just happen to be the same name in this case.
That means that you are passing the argument settings to Configurator which contains a variable called settings
here an example, you have a function:
def function_test(a=None, b=None, c=None):
pass
You could call it like that:
c = "something"
function_test(c=c)
which mean you passed the variable c that you created as an argument for the parameter c in the function function_test
args = [4,5]
kwargs = {'x': 7}
func(a, b=2, *args, **kwargs)
What is the accepted nomenclature for referring to a, b, args, and kwargs?
In particular, fill in the blank here:
'a' is a (_blank_) positional argument
'args' contains (_blank_) positional arguments
'b' is a (_blank_) keyword argument
'kwargs' contains (_blank_) keyword arguments
I'm looking for something like (blank) = {formal / arbitrary length / actual / variable position / fixed}
Also please correct me if I'm wrong on the above.
Much of what you're looking for can be found in the Python Tutorial.
From left to right:
a is a positional argument.
b=3 is a positional argument with a default value of 3.
*args is an arbitrary argument list, unpacked as a tuple.
**kwargs is a keyword argument, unpacked as a dictionary.
As Edward Loper already described, your terms are correct. You can only be more specific:
'a' is the first required positional argument.
'args' passes illegal positional arguments. (You know, since you use it after a keyword argument.)
'b' is an optional keyword argument… named b… with a default value of 2.
'kwargs' passes arbitrary keyword arguments.
def punk(a, b, *args, **kwargs):
print a
print b
print args
print kwargs
a=1; b=2; args = [4,5]; kwargs={'x':7}
punk(a, b=b, *args, **kwargs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: punk() got multiple values for keyword argument 'b'
If you use args legally, you could say it is used to pass potentially required and arbitrary positional arguments.
>>> punk(a, *args)
1
4
(5,)
{}