Many ways of Function Signature in python

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.


  1. There are three ways to define arguments in the function signature.

  2. Positional-only arguments are defined as follows:

     def __init__(name=None, /):

    The above function can only be called using the positional argument. No keyword argument is allowed for passing in 'name'

  3. Keyword-only arguments are defined as follows:

       def __init__(*, only=None, age=None):

    The above function can only be called using keyword argument. No positional argument is allowed.

  4. Positional-or-keyword arguments are defined as follows

    def __init_(name, age):

The above function can be called using either positional or keyword.

  1. 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.