Help(''.center) third argument?

In the help documentation for the string method ‘.center’, there appears to be three arguments:

center(width, fillchar=’ ', /) method of builtins.str instance

What is the forward slash referring to?

If function signatures, the forward slash separates arguments which can
only be given by position from those that can be given by position or
keyword.

It is mostly only builtin functions and methods written is C that
support purely positional arguments.

So in a Python function like this:

def center(string, width, fillchar=' ')

you can call the function with positional arguments:

center("Hello world", 100, '-')

or with keyword arguments:

center("Hello world", width=100, fillchar='-')

But in the real str.center method, being written in C, the developers
have not bothered to support keyword arguments. So even though the
function signature shows the parameter names “width” and “fillchar”, you
can’t use them by keyword.

The slash in the signature shows that.

By the way, there are also keyword only arguments that cannot be given
by position, only by keyword, and they are separated from the others
using an asterisk *.

So putting it altogether, we have:

def function( positional-only parameters, /,
              positional-or-keyword parameters, *, 
              keyword-only parameters ):

If you leave out the slash and the asterisk, all your parameters will be
positional-or-keyword.

Thank you, Steven (@steven.daprano), for your prompt and comprehensive reply.

How did you manage to anticipate my question about the asterisk, which I subsequently came across when looking at the list .sort() method? :grinning_face_with_smiling_eyes:

Because he runs a sophisticated deep leanring neural network inside his brain that has been trained on thousands of users just like you :laughing:

A few more things to note that you might run in to: appending a name (conventionally *args) to the * means the function accepts variditic positional args, i.e. zero or more additional arguments passed by position at the end of all other positionally-passed arguments, and passed as a list named args (or whatever you named it) for your function to use. For example,

def add(*numbers):
    sum = 0
    for number in numbers:
        sum += number
    return sum

(Of course, you would want to simply do sum(numbers), but for clarity about what numbers is I did it the long way).

There’s also **, most commonly **kwargs, which works similarly except it goes at the very end and collects all remaining arguments passed by keyword into a dictionary.

There is also parameters with and without a default arguments, which you are probably aware of, and also a seemingly pedantic but often quite point about terminology: parameters refers to the names you see in the function’s signature, e.g. width and fillchar, while arguments refers to what the caller passes to the function, e.g. 42 for width, or # for fillchar.

For more detail, the Python docs aren’t the best but this goes over the basics:

Cheers!

Ha, @CAM-Gerlach! I’m so predictable! Thanks for taking the time to reply with yet more interesting information. Time to work on my randomness function.

1 Like