In the python functions `all` and `any`, what does the second undocumented argument do?

71 views Asked by At

Background

The output of help(all) is:

Help on built-in function all in module builtins:

all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.

I went to look up the official python docs and the same function only has listed the first argument.

Question: What does the second undocumented argument do?

I think it is strange that the official docs do not list the second argument but the help() for the function does list the second argument. I also see the same second argument on other functions like any(). Also the second argument does not look like valid syntax because usually you can not have a variable named / because variables must be alpha-numeric/underscores characters.

2

There are 2 answers

0
S.B On

Actually / is not a parameter itself, but instead it indicates that the previous parameter(s) should be passed positional.

PEP 570 – Python Positional-Only Parameters

def fn(param, /):
    pass

fn(10)        # valid
fn(param=10)  # invalid
0
Marco Parola On

The second argument you see in the help(all) output, which is /, is a special notation in Python's function signature to indicate that the function only accepts positional arguments and not keyword arguments. This notation is used to restrict the way the function can be called and is not an actual argument that you provide when using the all() function.

So, in practice, when you use the all() function, you only provide a single argument, which is the iterable you want to check.

Similar concept * character.