Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is ,*, ?
#11
(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
Reply
#12
(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)
Reply
#13
I am confusing myself. Of course the default value of b is 'test'. But now I'm not sure what the purpose of * is.
Reply
#14
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
Here is the detailed description of five kinds of parameter I found: parameter
Reply
#16
(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'.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
im confused as on why you would want to force it?
Recommended Tutorials:
Reply
#18
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.
Reply
#19
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#20
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.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

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