Positional-only arguments in Python

Did you know?

You can force a user to call a function with positional arguments only using /.


def full_name(user, /):
    return f"{user['first_name']} {user['last_name']}"

print(full_name({"first_name": "Jan", "last_name": "Giamcomelli"}))
# => Jan Giamcomelli

print(full_name(user={"first_name": "Jan", "last_name": "Giamcomelli"}))
# => TypeError: full_name() got some positional-only arguments passed as keyword arguments: 'user'


Makes refactoring easier. You can change the name of your parameters without worrying about it breaking any code that uses the function.