Python Forum

Full Version: what is ,*, ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
in the documentation The Python Library Reference (at least in the PDF copy) many functions and class methods are shown with an argument list that has ,*, in it.  what does that mean?
do you mean variable arguments such as...

def func(*args)
or
def func(**kwargs)
?
No, he means ,*,. Check out the documentation for sorted. It uses that notation. I haven't figured out what it means yet.
a little while ago i was looking at the document for os.stat() and it also has ,*, shown.  i played around with it by adding an extra argument, but with 2 arguments it objected and said that 2 arguments is too many.  i then tried spliting the path into parts and gave it a list but it didn't like that, either.  google gives me no matches for it.
I always thought * was for unpacking, but that's surely not the case here.

I looked at sorted function code didn't find any use of *(except for the pointers in C).

May be it is to differentiate positional or keyword arguments. func(positional args,*, keyword args)

may be to represent that after ,*, no positional argument could be there.

Just a theory, let me know if you find a counter-example.

Ok so i tried to use ,*, in function defination and all it did is convert the argument on right to keyword argument.

>>> def myFunction(a, *, b):
...     return a+b
...
>>> myFunction(3,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: myFunction() takes 1 positional argument but 2 were given

>>> myFunction(3,b=5)
8
This '*' specifies an argument that can be supplied only by keyword.

Quote:keyword-only: specifies an argument that can be supplied only by keyword. Keyword-only parameters can be defined by including a single var-positional parameter or bare * in the parameter list of the function definition before them, for example kw_only1 and kw_only2 in the following:

def func(arg, *, kw_only1, kw_only2): ...
Good to know.

Im not sure when i would use that. kwargs were always the last parameters given. And ifi wanted to signify that it was indeed kwargs, i would put a param=None or something.
def test(a, b='test'):
    print(a)
    print(b)

def test2(a, *, b='test'):
    print(a)
    print(b)
    
test('hello', 'world')
test2('hello', b='world')
If you write: test2('hello', 'world')
you get: TypeError: test2() takes 1 positional argument but 2 were given

https://www.python.org/dev/peps/pep-3102/
It looks like the name of the function is automatically passed as argument b:
>>> def test2(a, *, b='test'):
...     print(a)
...     print(b)
...
>>> test2('hello')
result:
Output:
hello test >>>
(Sep-09-2017, 12:26 PM)syogun Wrote: [ -> ]This '*' specifies an argument that can be supplied only by keyword.
Quote:keyword-only: specifies an argument that can be supplied only by keyword. Keyword-only parameters can be defined by including a single var-positional parameter or bare * in the parameter list of the function definition before them, for example kw_only1 and kw_only2 in the following:
def func(arg, *, kw_only1, kw_only2): ...
 
so, where ,*, is not used, the documentation is saying that these arguments, or those before the ,*,, can be provided by either a named option or be unnamed in the position shown?

so, is this how to code a function prototype to do that?

def f(positional, either_way_with_default_value='foo', *, only_as_a_named_option='bar'): ...
???

so how can a definition of an argument specifying that it can be given either way define the difference between a required argument and one that is not required (e.g. calling f() above with just one positional  argument)?
Pages: 1 2 3