Posts: 7,325
Threads: 123
Joined: Sep 2016
(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
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
Posts: 7,325
Threads: 123
Joined: Sep 2016
(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)
Posts: 12,050
Threads: 487
Joined: Sep 2016
I am confusing myself. Of course the default value of b is 'test'. But now I'm not sure what the purpose of * is.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 18
Threads: 2
Joined: Aug 2017
Here is the detailed description of five kinds of parameter I found: parameter
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
(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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
im confused as on why you would want to force it?
Recommended Tutorials:
Posts: 18
Threads: 2
Joined: Aug 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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
|