Python Forum

Full Version: What do * and / mean?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

The reference of math.module says about the prod function like this:

Quote: math.prod(iterable, *, start=1)

Calculate the product of all the elements in the input iterable. The default start value for the product is 1.

When the iterable is empty, return the start value. This function is intended specifically for use with numeric values and may reject non-numeric types.

However, the help function shows like this:

help(math.prod)
Help on built-in function prod in module math:

prod(iterable, /, *, start=1)
    Calculate the product of all the elements in the input iterable.
    
    The default start value for the product is 1.
    
    When the iterable is empty, return the start value.  This function is
    intended specifically for use with numeric values and may reject
    non-numeric types.
What do * and / mean in the argument list of prod?
At first, I thought prod is something like higher-order function, but it is not.
I tested to give * and / to it, but errors occur.
What do they mean?

Thanx.
I suggest to read documentation Function definitions

Quote: When one or more parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

Quote: Parameters before “/” are positional-only parameters and may only be passed by positional arguments.
PEP 570 is also worth reading about this matter.
(Dec-11-2022, 06:53 AM)perfringo Wrote: [ -> ]I suggest to read documentation Function definitions

Quote: When one or more parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

Quote: Parameters before “/” are positional-only parameters and may only be passed by positional arguments.

(Dec-11-2022, 06:56 AM)Gribouillis Wrote: [ -> ]PEP 570 is also worth reading about this matter.

Oh, I see. These are separators!

Thank you, both guys, Mr. perfringo and Mr. Gribouillis.
I really appreciate you guys!