Keyword arguments

In production code, this function call is not particularly self documenting. We can improve that situation by naming the border argument at the call site:

>>> banner("Sun, Moon and Stars", border="*")
*******************
Sun, Moon and Stars
*******************

In this case the message string is called a "positional argument" and the border string a "keyword argument". In a call, the positional arguments are matched up in sequence with the formal arguments declared in the function definition. The keyword arguments, on the other hand, are matched by name. If we use keyword arguments for both of our parameters, we have the freedom to supply them in any order:

>>> banner(border=".", message="Hello from Earth")
................
Hello from Earth
................

Remember, though, that all keyword arguments must be specified after any positional arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset