Sep-09-2017, 01:45 AM
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?,*,
in it. what does that mean?def func(*args)
def func(**kwargs)
,*,
. Check out the documentation for sorted. It uses that notation. I haven't figured out what it means yet.,*,
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.*
was for unpacking, but that's surely not the case here.*
(except for the pointers in C).func(positional args,*, keyword args)
,*,
no positional argument could be there.,*,
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
'*'
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): ...
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')
>>> def test2(a, *, b='test'): ... print(a) ... print(b) ... >>> test2('hello')result:
Output:hello
test
>>>
(Sep-09-2017, 12:26 PM)syogun Wrote: [ -> ]Thisso, where'*'
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): ...
,*,
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?def f(positional, either_way_with_default_value='foo', *, only_as_a_named_option='bar'): ...???