Now that we understand the distinction between object references and objects, we'll look at some more capabilities of function arguments.
The formal function arguments specified when a function is defined with the def
keyword are a comma-separated list of the argument names. These arguments can be made optional by providing default values. Consider a function which prints a simple banner to the console:
>>> def banner(message, border='-'): ... line = border * len(message) ... print(line) ... print(message) ... print(line) ...
This function takes two arguments, and we provide a default value — in this case '-'
— in a literal string. When we define functions using default arguments, the parameters with default arguments must come after those without defaults, otherwise we will get a SyntaxError
.
On line 2 of the function we multiply our border string by the length of the message string. This line shows two interesting...