Page 136 -
P. 136
functions
Q: Can I still call the Twitter function like this: send_to_ Q: Doesn’t all that optional stuff get kinda confusing?
twitter()? Or do I always have to provide a value for the msg
parameter? A: Sometimes. As you create and use functions, you’ll get a
feel for when to make parameters mandatory and when to make
A: As it’s written, the parameter is required by the function. If them optional. If you look at the description of print() again ,
you leave it out, Python will complain and refuse to run your code you’ll see that in most usage scenarios print() takes a single
further. parameter: the thing to display. It is only when extra, less common,
Q: Can parameters to functions be optional? functionality is required that the other parameters are needed.
Q:
The description of print() mentions “keyword
A: Yes. In most programming languages (including Python), you arguments.” What are they?
can provide a default value for a parameter, which is then used if
the calling code doesn’t provide any value. This has the effect of A: The word “argument” is another name for “parameter,” and
making the parameter optional, in that it either takes its value from it means the same thing. In Python, an argument can have
the one provided by the caller, or uses the default value if the caller an optional “keyword” associated with it. This means that the
does not provide anything. parameter has been given a name that the calling code can use to
Q: Can there be more than one parameter? identify which value in its code is associated with which parameter
in the function.
A: Yes, you can have as many as you like. Just bear in mind that Continuing to use print() as an example, the sep, end,
a function with a gazillion parameters can be hard to understand, and file parameters (a.k.a. keyword arguments) each have
let alone use. a default value, so they are all optional. However, if you need to
Q: use only one of them in the calling code, you need some way to
identify which one you are using, and that’s where the keyword
Can all the parameters be optional?
arguments come in. There are examples of these optional features
of print() and other such functions later in the book. Don’t
A: Yes. As an example, Python’s built-in print() function sweat the details right now, though.
can have up to three optional parameters, in addition to the stuff to
print (which is also optional). To learn more, open up a Python Shell
prompt and type help(print) at the >>> prompt.
you are here 4 101