I have always been of the mind that explicit is much better than implicit. For this reason, I have almost always included Python’s keyword-only arguments when defining a function.
My reasoning behind this is I feel that I should always know what I am passing into a function and how it’s intended to be used. Below is an example:
def divide(*, numerator: int, denominator: int) -> float:
Divide numerator by denominator, return float.
In this example, you must define which parameter use named keyword arguments, eg
divide(numerator=100, denominator=50). If you try calling the function without doing this eg
divide(100,50), you will get the following error:
TypeError: divide() takes 0 positional arguments but 2 were given. This forces the user to be explicit.
However, this is only a good strategy when your variable names carry meaning like in the division example. Not all variables do carry important information. However, we can see this in many built-in python functions for example,
len. The name of the variable in len's definition is
obj. This makes perfect sense since we are trying to find the length of an object. Because this is implicit and well-known knowledge, it would be silly to force a user to pass in
obj when calling
Now you could say, in the case of functions like
len, let's just not include the keyword-only argument. Doing so means that the user does not have to define it as a keyword, but a user still can. Giving a user the ability to do this can have some detrimental effects. It means that users can call the function in two different ways, either
len(obj=list_object). Why not be explicit in how you want your function to be called?
This is where Python3.8’s Positional-only argument comes in! As a part of PEP 570 the
/ argument. This argument will force all arguments preceding it, to be positional only, meaning that the user must provide them as positional arguments and not give them as keyword arguments.
It is important to remember that it is all elements preceding the /
Interesting bit of trivia on the choice of the
Alternative proposal: how about using ‘/’ ? It’s…