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


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

(Sep-09-2017, 11:03 PM)Larz60+ Wrote: It looks like the name of the function is automatically passed as argument b:
No,default argument test is passed.
Name of function is test2 Confused

The point is as dvs1 showed.
If use that function without ,*,,and pass in 2 positional argument.
def test1(a, b='test'):
    print(a)
    print(b
     
test1('hello', 'world')
Output:
hello world
So is allowed to give a argument that is not a keyword argumet.
With ,*,
def test1(a,*, b='test'):
    print(a)
    print(b)
     
test1('hello', 'world')
Output:
Traceback (most recent call last): File "e:\1py_div\div_code\pos.py", line 32, in <module> test1('hello', 'world') TypeError: test1() takes 1 positional argument but 2 were given
So is forced to use keyword argument.
def test1(a,*, b='test'):
    print(a)
    print(b)
     
test1('hello', b='world')
Output:
hello world



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

(Sep-10-2017, 12:23 AM)Skaperen Wrote: 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)?
It's only forcing that last argument has to be a key word argument.
λ ptpython
>>> def f(positional, either_way_with_default_value='foo',*, only_as_a_named_option='bar'):
...     return positional,either_way_with_default_value,only_as_a_named_option

>>> f(1)
(1, 'foo', 'bar')

>>> f(1, 2)
(1, 2, 'bar')

>>> f(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
f() takes from 1 to 2 positional arguments but 3 were given

>>> f(1, 2, only_as_a_named_option=3)
(1, 2, 3)
If remove ,*,.
>>> def f(positional, either_way_with_default_value='foo', only_as_a_named_option='bar'):
...     return positional,either_way_with_default_value,only_as_a_named_option

>>> f(1)
(1, 'foo', 'bar')

>>> f(1, 2)
(1, 2, 'bar')

>>> f(1, 2, 3)
(1, 2, 3)



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

I am confusing myself. Of course the default value of b is 'test'. But now I'm not sure what the purpose of * is.


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

(Sep-10-2017, 01:50 AM)Larz60+ Wrote: I am confusing myself. Of course the default value of b is 'test'. But now I'm not sure what the purpose of * is.

It makes the parameters after it keyword only parameters. Those parameters cannot be supplied as positional parameters.


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

Here is the detailed description of five kinds of parameter I found: parameter


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

(Sep-09-2017, 11:03 PM)Larz60+ Wrote: It looks like the name of the function is automatically passed as argument b:
or rather it is the default value for b as given in the def that has b='test'.


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

im confused as on why you would want to force it?


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

PEP 3102 explains why they introduce the keyword-only arguments. My English is so poor that I can't get it all. But I think here is the reason. Maybe someone can explain better with some examples.


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

The rationale in the PEP doesn't make sense. It says it's there so you can have keyword arguments after *args without having to resort to **kwargs. So they propose changing the syntax to allow that (which is okay, and works currently), but then add in the plain * parameter to force keyword-only arguments, without giving a rationale for keyword-only arguments.


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

This was created in 2006? I dont think i have ever seen code use that syntax before then.

I still dont understand its purpose, as kwargs were always assigned the last parameters in a function. There doesnt seem to be any reason to force it then.