27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Python</strong>htp1_04.fm Page 147 Saturday, December 8, 2001 9:34 AM<br />

Chapter 4 Functions 147<br />

11 if CGI == "yes":<br />

12 print "CGI scripts are enabled"<br />

13 print # prints a new line<br />

14<br />

15 generateWebsite( "<strong>Deitel</strong>" )<br />

16<br />

17 generateWebsite( "<strong>Deitel</strong>", Flash = "yes",<br />

18 url = "www.deitel.com/new" )<br />

19<br />

20 generateWebsite( CGI = "no", name = "Prentice Hall" )<br />

Generating site requested by <strong>Deitel</strong> using url www.deitel.com<br />

CGI scripts are enabled<br />

Generating site requested by <strong>Deitel</strong> using url www.deitel.com/new<br />

Flash is enabled<br />

CGI scripts are enabled<br />

Generating site requested by Prentice Hall using url www.deitel.com<br />

Fig. Fig. 4.21 4.21 Keyword parameters. (Part 2 of 2.)<br />

Function generateWebsite takes four arguments. The keyword argument names<br />

url, Flash and CGI are assigned the default values "www.deitel.com", "no" and<br />

"yes", respectively (lines 4–5). The function identifies who is requesting the Web site and<br />

displays a message if the Web site is Flash- or CGI-enabled (lines 6–13).<br />

The function call in line 15 passes one argument, a value for name, to function<br />

generateWebsite. The function uses the default values given in the definition for the<br />

other parameters.<br />

The function call in lines 17–18 passes three arguments to generateWebsite.<br />

Variable name again has the value "<strong>Deitel</strong>". The call also assigns the value "yes" to<br />

keyword argument Flash and "www.deitel.com/new" to keyword argument url.<br />

This function call illustrates that the order of keyword arguments is more flexible than that<br />

of regular arguments in an ordinary function call. The <strong>Python</strong> interpreter matches the value<br />

"<strong>Deitel</strong>" with variable name by its position in the function call. The <strong>Python</strong> interpreter<br />

matches the values passed to url and Flash by their keyword argument names rather<br />

than by their positions in the function call. The value of name must come first in any call<br />

to generateWebsite if it is not referenced by specifying a value for name in the argument<br />

list. Line 20 demonstrates that any function argument can be referenced as a keyword<br />

even if it has no default value.<br />

The interactive session of Fig. 4.22 demonstrates common errors when mixing nonkeyword<br />

and keyword arguments. Function call test( number1 = "two", "Name" )<br />

causes an error, because the non-keyword argument is placed after the keyword argument.<br />

Function call test( number1 = "three" ) is incorrect, because function test expects<br />

one non-keyword argument.<br />

Common <strong>Program</strong>ming Error 4.10<br />

Misplacing or omitting the value for a non-keyword argument in a function call is an error. 4.10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!