Many ways of Function Signature in python
2 min read
Recently I was looking through the implementation of
Counter implementation in the standard library. It was interesting to see the following function signature in the constructor:
def __init__(self, iterable=None, /, **kwargs): ....
This peculiar forward-slash(/) caught my attention. Experimenting a few times, I realized that this is a way to declare positional only arguments. Turns out, this is the exact opposite of keyword-only arguments. Once I realized the opposite nature compared to keyword-only arguments, it finally made sense.
There are three ways to define arguments in the function signature.
Positional-only arguments are defined as follows:
def __init__(name=None, /): pass
The above function can only be called using the positional argument. No keyword argument is allowed for passing in 'name'
Keyword-only arguments are defined as follows:
def __init__(*, only=None, age=None): pass
The above function can only be called using keyword argument. No positional argument is allowed.
Positional-or-keyword arguments are defined as follows
def __init_(name, age): pass
The above function can be called using either positional or keyword.
- Real-world use case mix and match between the above approach for their use case. Sample function signature could be:
def create_list(iterable, /, *, cache=True): print(iterable, cache)
The above function requires 'cache' to always be keyword and 'iterable' be always postional.