Python Forum
what is ,*, ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: what is ,*, ? (/thread-4791.html)

Pages: 1 2 3


what is ,*, ? - Skaperen - Sep-09-2017

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?


RE: what is ,*, ? - metulburr - Sep-09-2017

do you mean variable arguments such as...

def func(*args)
or
def func(**kwargs)
?


RE: what is ,*, ? - ichabod801 - Sep-09-2017

No, he means ,*,. Check out the documentation for sorted. It uses that notation. I haven't figured out what it means yet.


RE: what is ,*, ? - Skaperen - Sep-09-2017

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.


RE: what is ,*, ? - hbknjr - Sep-09-2017

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



RE: what is ,*, ? - syogun - Sep-09-2017

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): ...



RE: what is ,*, ? - metulburr - Sep-09-2017

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.


RE: what is ,*, ? - dvs1 - Sep-09-2017

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/


RE: what is ,*, ? - Larz60+ - Sep-09-2017

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 >>>



RE: what is ,*, ? - Skaperen - Sep-10-2017

(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)?