1. Clarify function calls with keyword arguments
When passing values to a function, prefer keyword arguments when they make the call clearer.
Keyword arguments turn positional knowledge into explicit names at the call site, which is especially helpful when several parameters share the same broad type. They also make later refactors safer because reviewers can see which argument is intended to mean what.
1.1. Don’t do this
1def format_information(first_name, last_name, age):
2 return '{} {} is {} years old'.format(first_name, last_name, age)
3
4format_information('John', 'Doe', 28)
1.2. Do this
1def format_information(first_name, last_name, age):
2 return '{} {} is {} years old'.format(first_name, last_name, age)
3
4format_information(first_name='John', last_name='Doe', age=28)
1def format_information(first_name, last_name, age):
2 return '{} {} is {} years old'.format(first_name, last_name, age)
3
4format_information(**{
5 'first_name': 'John',
6 'last_name': 'Doe',
7 'age': 28
8})