Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is ,*, ?
#21
It's used in some internal functions, like list.sort().  The idea behind having arguments which are keyword only is to make code clearer. 

For example, if you pass a function to list.sort, does that function take two values and return which should be sorted higher?  You'd have to read the code and try to infer what was happening, or look at the docs for list.sort() and see what it says.  By simply forcing keyword arguments, you'd immediately see that code written like list.sort(key=some_function) is using a function to extract the value which is to be sorted for each item in the list, instead of actually doing the sorting itself.

The only real use case is code clarity.  It doesn't help you do anything you couldn't already do.  Use it only to help reduce ambiguity.
Reply
#22
i think i get it now (after reading the PEP).

in def's function prototype, you can have an argument like foo='bar' at a specific position.  when calling that function, you can provide a value either in that same position without a name or in any ending position by name.  all named values must follow all positioned values (a positioned value may not follow a named value ... after named values, positions are ambiguous).  what the * argument does is change the argument list from that point on so that their defined positions do not have a positional meaning ... to provide a value, the call must provide it as name=value (what are typically called "options" even if we make them mandatory).

Output:
lt1/forums /home/forums 3> py3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def f(a,b,*,c=1): ...     return a,b,c ... >>> f(1,2,3) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: f() takes 2 positional arguments but 3 were given >>> f(1,2,c=3) (1, 2, 3) >>> lt1/forums /home/forums 4>
this does not provide for a means to have an argument be position-only and have a default value if not provided.  that is already provided for as variable argument lists.

also, there is no means i am aware of that provides for a named argument that has no default value, and as a result of that must be provided in a call as a name=value form.  i propose enabling this by allowing the argument prototype to have arguments to do this in the form name=
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#23
i have now added ,* to a couple of my functions where it only made sense to provide the named arguments as named by the caller.  it works.  one function had a single named argument and no others.  i did not add ,* to that one.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#24
I have written a code to understand the use of bare "*" in function argument list and I am sharing that with everyone.

I have created a function called myFunction having three arguments "a","b" and "c". I placed "*" between a and b
def myFunction(a,*,b,c):
	print('a',a)
	print('b',b)
	print('c',c)

myFunction(10,c=20,b=12)
Output:
Output:
a 10 b 12 c 20
The "*" in argument list of a function ensures that the arguments after that are Non-positional
Non-positional argument means that the order of that argument during function call is not fixed and value is passed using key of that argument.
The arguments before * are positional arguments and values are passed to these argument without keys.

Another code:
def myFunction(*,a,b,c):
	print('a',a)
	print('b',b)
	print('c',c)

myFunction(a=10,c=20,b=12)
Reply
#25
to add to the above:
Output:
Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def f(a,*,b,c,d,e,f): ...   return ... >>> f(1,b=2) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: f() missing 4 required keyword-only arguments: 'c', 'd', 'e', and 'f' >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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