Posts: 4,599
Threads: 1,479
Joined: Sep 2016
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.
Posts: 5,150
Threads: 396
Joined: Sep 2016
do you mean variable arguments such as...
def func(*args)
or
def func(**kwargs)
?
Recommended Tutorials:
Posts: 4,229
Threads: 97
Joined: Sep 2016
No, he means ,*, . Check out the documentation for sorted. It uses that notation. I haven't figured out what it means yet.
Posts: 4,599
Threads: 1,479
Joined: Sep 2016
Sep-09-2017, 04:02 AM
(This post was last modified: Sep-09-2017, 04:03 AM by Skaperen.)
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.
Posts: 101
Threads: 7
Joined: Aug 2017
Sep-09-2017, 07:24 AM
(This post was last modified: Sep-09-2017, 07:33 AM by hbknjr.)
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
Posts: 18
Threads: 2
Joined: Aug 2017
Sep-09-2017, 12:26 PM
(This post was last modified: Sep-09-2017, 12:36 PM by syogun.)
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): ...
Posts: 5,150
Threads: 396
Joined: Sep 2016
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:
Posts: 16
Threads: 9
Joined: Sep 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/
Posts: 11,890
Threads: 475
Joined: Sep 2016
Sep-09-2017, 11:03 PM
(This post was last modified: Sep-09-2017, 11:03 PM by Larz60+.)
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
>>>
Posts: 4,599
Threads: 1,479
Joined: Sep 2016
(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.
|