Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is ,*, ?
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
do you mean variable arguments such as...

def func(*args)
or
def func(**kwargs)
?
Recommended Tutorials:
Reply
#3
No, he means ,*,. Check out the documentation for sorted. It uses that notation. I haven't figured out what it means yet.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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
Reply
#6
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): ...
Reply
#7
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.
Recommended Tutorials:
Reply
#8
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/
Reply
#9
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 >>>
Reply
#10
(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)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020