Skip to content

09 - Exploring special function parameters

Starting from Python 3.8 it was added special function parameters that are very handy for interface designers.

Parameter x Argument

When you declare a function, the function may or may not have parameters.

When you call a function, you may call it with or without arguments.

Special parameters

Positional-only arguments

def positional_only(x=1, y=1, z=1, /):
    pass

# positional_only(1,1,z=2) 
# Error

Keyword-only arguments

The good thing about this is that you force the user of your library to write the keyword arguments. You don't need to remember the order of the parameters. Moreover, you can extend, change the signature (up to some constraints) without penalizing previous utilizations of the library.

def keyword_only(*, x=1, y=1, z=1):
    pass

Use cases

ages = {}

help(ages.get)
help(subprocess.Popen)