Archive:Think Python/Functions: Difference between revisions
wikademia>Whiteknight m Think Python: Automatically uploading HTML source of this book from http://www.greenteapress.com/thinkpython/html/. Will convert to wikitext in a separate step |
wikademia>Wikademia |
||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{Think Python/Page}} | |||
{{?}} | |||
== Chapter 3  Functions == | |||
=== 3.1  Function calls === | |||
In the context of programming, a '''function''' is a named sequence of | |||
statements that performs a computation. When you define a function, | statements that performs a computation. When you define a function, | ||
you specify the name and the sequence of statements. Later, you can | you specify the name and the sequence of statements. Later, you can | ||
“call” the function by name. | “call” the function by name. | ||
We have already seen one example of a | We have already seen one example of a '''function call''': | ||
<PRE CLASS="verbatim">>>> type(32) | |||
<type 'int'> | <type 'int'> | ||
</PRE> | |||
is called the | The name of the function is <TT>type</TT>. The expression in parentheses | ||
function, is the type of the argument. | is called the '''argument''' of the function. The result, for this | ||
a result. The result is called the | function, is the type of the argument. | ||
It is common to say that a function “takes” an argument and “returns” | |||
a result. The result is called the '''return value'''. | |||
from one type to another. The | |||
converts it to an integer, if it can, or complains otherwise: | |||
=== 3.2  Type conversion functions === | |||
Python provides built-in functions that convert values | |||
from one type to another. The <TT>int</TT> function takes any value and | |||
converts it to an integer, if it can, or complains otherwise: | |||
<PRE CLASS="verbatim">>>> int('32') | |||
32 | 32 | ||
>>> int('Hello') | >>> int('Hello') | ||
ValueError: invalid literal for int(): Hello | ValueError: invalid literal for int(): Hello | ||
</PRE> | |||
doesn’t round off; it chops off the fraction part: | <TT>int</TT> can convert floating-point values to integers, but it | ||
doesn’t round off; it chops off the fraction part: | |||
<PRE CLASS="verbatim">>>> int(3.99999) | |||
3 | 3 | ||
>>> int(-2.3) | >>> int(-2.3) | ||
-2 | -2 | ||
</PRE> | |||
numbers: | <TT>float</TT> converts integers and strings to floating-point | ||
numbers: | |||
<PRE CLASS="verbatim">>>> float(32) | |||
32.0 | 32.0 | ||
>>> float('3.14159') | >>> float('3.14159') | ||
3.14159 | 3.14159 | ||
</PRE> | |||
Finally, <TT>str</TT> converts its argument to a string: | |||
<PRE CLASS="verbatim">>>> str(32) | |||
'32' | '32' | ||
>>> str(3.14159) | >>> str(3.14159) | ||
'3.14159' | '3.14159' | ||
</PRE>=== 3.3  Math functions === | |||
mathematical functions. A | |||
collection of related functions. | |||
Python has a math module that provides most of the familiar | |||
you print the module object, you get some information about it: | mathematical functions. A '''module''' is a file that contains a | ||
collection of related functions. | |||
Before we can use the module, we have to import it: | |||
<PRE CLASS="verbatim">>>> import math | |||
</PRE> | |||
This statement creates a '''module object''' named math. If | |||
you print the module object, you get some information about it: | |||
<PRE CLASS="verbatim">>>> print math | |||
<module 'math' from '/usr/lib/python2.5/lib-dynload/math.so'> | <module 'math' from '/usr/lib/python2.5/lib-dynload/math.so'> | ||
</PRE> | |||
The module object contains the functions and variables defined in the | |||
module. To access one of the functions, you have to specify the name | module. To access one of the functions, you have to specify the name | ||
of the module and the name of the function, separated by a dot (also | of the module and the name of the function, separated by a dot (also | ||
known as a period). This format is called | known as a period). This format is called '''dot notation'''. | ||
<PRE CLASS="verbatim">>>> ratio = signal_power / noise_power | |||
>>> decibels = 10 * math.log10(ratio) | >>> decibels = 10 * math.log10(ratio) | ||
>>> radians = 0.7 | >>> radians = 0.7 | ||
>>> height = math.sin(radians) | >>> height = math.sin(radians) | ||
</PRE> | |||
The first example computes the logarithm base 10 of the | |||
signal-to-noise ratio. The math module also provides a | signal-to-noise ratio. The math module also provides a | ||
function called | function called <TT>log</TT> that computes logarithms base <TT>e</TT>. | ||
variable is a hint that | |||
functions ( | |||
convert from degrees to radians, divide by 360 and multiply by | |||
π | The second example finds the sine of <TT>radians</TT>. The name of the | ||
variable is a hint that <TT>sin</TT> and the other trigonometric | |||
functions (<TT>cos</TT>, <TT>tan</TT>, etc.) take arguments in radians. To | |||
convert from degrees to radians, divide by 360 and multiply by 2 | |||
π: | |||
<PRE CLASS="verbatim">>>> degrees = 45 | |||
>>> radians = degrees / 360.0 * 2 * math.pi | >>> radians = degrees / 360.0 * 2 * math.pi | ||
>>> math.sin(radians) | >>> math.sin(radians) | ||
0.707106781187 | 0.707106781187 | ||
</PRE> | |||
The expression <TT>math.pi</TT> gets the variable <TT>pi</TT> from the math | |||
module. The value of this variable is an approximation | module. The value of this variable is an approximation | ||
of | of π, accurate to about 15 digits. | ||
If you know | |||
your trigonometry, you can check the previous result by comparing it to | your trigonometry, you can check the previous result by comparing it to | ||
the square root of two divided by two: | the square root of two divided by two: | ||
<PRE CLASS="verbatim">>>> math.sqrt(2) / 2.0 | |||
0.707106781187 | 0.707106781187 | ||
</PRE>=== 3.4  Composition === | |||
So far, we have looked at the elements of a program—variables, | |||
expressions, and statements—in isolation, without talking about how to | expressions, and statements—in isolation, without talking about how to | ||
combine them. | combine them. | ||
ability to take small building blocks and | |||
One of the most useful features of programming languages is their | |||
ability to take small building blocks and '''compose''' them. For | |||
example, the argument of a function can be any kind of expression, | example, the argument of a function can be any kind of expression, | ||
including arithmetic operators: | including arithmetic operators: | ||
<PRE CLASS="verbatim">x = math.sin(degrees / 360.0 * 2 * math.pi) | |||
</PRE> | |||
And even function calls: | |||
<PRE CLASS="verbatim">x = math.exp(math.log(x+1)) | |||
</PRE> | |||
Almost anywhere you can put a value, you can put an arbitrary | |||
expression, with one exception: the left side of an assignment | expression, with one exception: the left side of an assignment | ||
statement has to be a variable name. Any other expression on the left | statement has to be a variable name. Any other expression on the left | ||
side is a syntax error | side is a syntax error<SUP>1</SUP>. | ||
<PRE CLASS="verbatim">>>> minutes = hours * 60 # right | |||
>>> hours * 60 = minutes # wrong! | >>> hours * 60 = minutes # wrong! | ||
SyntaxError: can't assign to operator | SyntaxError: can't assign to operator | ||
</PRE> | |||
=== 3.5  Adding new functions === | |||
So far, we have only been using the functions that come with Python, | |||
but it is also possible to add new functions. | but it is also possible to add new functions. | ||
A | A '''function definition''' specifies the name of a new function and | ||
the sequence of statements that execute when the function is called. | the sequence of statements that execute when the function is called. | ||
Here is an example: | |||
<PRE CLASS="verbatim">def print_lyrics(): | |||
print "I'm a lumberjack, and I'm okay." | print "I'm a lumberjack, and I'm okay." | ||
print "I sleep all night and I work all day." | print "I sleep all night and I work all day." | ||
</PRE> | |||
definition. The name of the function is | <TT>def</TT> is a keyword that indicates that this is a function | ||
definition. The name of the function is <CODE>print_lyrics</CODE>. The | |||
rules for function names are the same as for variable names: letters, | rules for function names are the same as for variable names: letters, | ||
numbers and some punctuation marks are legal, but the first character | numbers and some punctuation marks are legal, but the first character | ||
can’t be a number. You can’t use a keyword as the name of a function, | can’t be a number. You can’t use a keyword as the name of a function, | ||
and you should avoid having a variable and a function with the same | and you should avoid having a variable and a function with the same | ||
name. | name. | ||
doesn’t take any arguments. | |||
The empty parentheses after the name indicate that this function | |||
doesn’t take any arguments. | |||
the rest is called the | |||
The first line of the function definition is called the '''header'''; | |||
the rest is called the '''body'''. The header has to end with a colon | |||
and the body has to be indented. By convention, the indentation is | and the body has to be indented. By convention, the indentation is | ||
always four spaces (see Section  | always four spaces (see Section 3.13). The body can contain | ||
any number of statements. | any number of statements. | ||
The strings in the print statements are enclosed in double | |||
quotes. Single quotes and double quotes do the same thing; | quotes. Single quotes and double quotes do the same thing; | ||
most people use single quotes except in cases like this where | most people use single quotes except in cases like this where | ||
a single quote (which is also an apostrophe) appears in the string. | a single quote (which is also an apostrophe) appears in the string. | ||
prints ellipses ( | |||
isn’t complete: | If you type a function definition in interactive mode, the interpreter | ||
prints ellipses (''...'') to let you know that the definition | |||
isn’t complete: | |||
<PRE CLASS="verbatim">>>> def print_lyrics(): | |||
... print "I'm a lumberjack, and I'm okay." | ... print "I'm a lumberjack, and I'm okay." | ||
... print "I sleep all night and I work all day." | ... print "I sleep all night and I work all day." | ||
... | ... | ||
</PRE> | |||
not necessary in a script). | To end the function, you have to enter an empty line (this is | ||
not necessary in a script). | |||
Defining a function creates a variable with the same name. | |||
<PRE CLASS="verbatim">>>> print print_lyrics | |||
<function print_lyrics at 0xb7e99e9c> | <function print_lyrics at 0xb7e99e9c> | ||
>>> print type(print_lyrics) | >>> print type(print_lyrics) | ||
<type 'function'> | <type 'function'> | ||
</PRE> | |||
has type | The value of <CODE>print_lyrics</CODE> is a '''function object''', which | ||
has type <CODE>'function'</CODE>. | |||
for built-in functions: | |||
The syntax for calling the new function is the same as | |||
for built-in functions: | |||
<PRE CLASS="verbatim">>>> print_lyrics() | |||
I'm a lumberjack, and I'm okay. | I'm a lumberjack, and I'm okay. | ||
I sleep all night and I work all day. | I sleep all night and I work all day. | ||
</PRE> | |||
Once you have defined a function, you can use it inside another | |||
function. For example, to repeat the previous refrain, we could write | function. For example, to repeat the previous refrain, we could write | ||
a function called | a function called <CODE>repeat_lyrics</CODE>: | ||
<PRE CLASS="verbatim">def repeat_lyrics(): | |||
print_lyrics() | print_lyrics() | ||
print_lyrics() | print_lyrics() | ||
</PRE> | |||
And then call <CODE>repeat_lyrics</CODE>: | |||
<PRE CLASS="verbatim">>>> repeat_lyrics() | |||
I'm a lumberjack, and I'm okay. | I'm a lumberjack, and I'm okay. | ||
I sleep all night and I work all day. | I sleep all night and I work all day. | ||
I'm a lumberjack, and I'm okay. | I'm a lumberjack, and I'm okay. | ||
I sleep all night and I work all day. | I sleep all night and I work all day. | ||
</PRE> | |||
But that’s not really how the song goes. | |||
whole program looks like this: | === 3.6  Definitions and uses === | ||
Pulling together the code fragments from the previous section, the | |||
whole program looks like this: | |||
<PRE CLASS="verbatim">def print_lyrics(): | |||
print "I'm a lumberjack, and I'm okay." | print "I'm a lumberjack, and I'm okay." | ||
print "I sleep all night and I work all day." | print "I sleep all night and I work all day." | ||
| Line 172: | Line 271: | ||
repeat_lyrics() | repeat_lyrics() | ||
</PRE> | |||
This program contains two function definitions: <CODE>print_lyrics</CODE> and | |||
<CODE>repeat_lyrics</CODE>. Function definitions get executed just like other | |||
statements, but the effect is to create function objects. The statements | statements, but the effect is to create function objects. The statements | ||
inside the function do not get executed until the function is called, and | inside the function do not get executed until the function is called, and | ||
the function definition generates no output. | the function definition generates no output. | ||
As you might expect, you have to create a function before you can | |||
execute it. In other words, the function definition has to be | execute it. In other words, the function definition has to be | ||
executed before the first time it is called. | executed before the first time it is called. | ||
<DIV CLASS="theorem">'''Exercise 1'''  '' | |||
Move the last line of this program | Move the last line of this program | ||
to the top, so the function call appears before the definitions. Run | to the top, so the function call appears before the definitions. Run | ||
the program and see what error | the program and see what error | ||
message you get. | message you get. | ||
''</DIV><DIV CLASS="theorem">'''Exercise 2'''  '' | |||
Move the function call back to the bottom | Move the function call back to the bottom | ||
and move the definition of | and move the definition of ''<CODE>''print_lyrics''</CODE>'' after the definition of | ||
''<CODE>''repeat_lyrics''</CODE>''. What happens when you run this program? | |||
''</DIV>=== 3.7  Flow of execution === | |||
In order to ensure that a function is defined before its first use, | |||
you have to know the order in which statements are executed, which is | you have to know the order in which statements are executed, which is | ||
called the | called the '''flow of execution'''. | ||
Statements are executed one at a time, in order from top to bottom. | |||
Execution always begins at the first statement of the program. | |||
Statements are executed one at a time, in order from top to bottom. | |||
Function definitions do not alter the flow of execution of the | |||
program, but remember that statements inside the function are not | program, but remember that statements inside the function are not | ||
executed until the function is called. | executed until the function is called. | ||
A function call is like a detour in the flow of execution. Instead of | |||
going to the next statement, the flow jumps to the body of | going to the next statement, the flow jumps to the body of | ||
the function, executes all the statements there, and then comes back | the function, executes all the statements there, and then comes back | ||
to pick up where it left off. | to pick up where it left off. | ||
That sounds simple enough, until you remember that one function can | |||
call another. While in the middle of one function, the program might | call another. While in the middle of one function, the program might | ||
have to execute the statements in another function. But while | have to execute the statements in another function. But while | ||
executing that new function, the program might have to execute yet | executing that new function, the program might have to execute yet | ||
another function! | another function! | ||
Fortunately, Python is good at keeping track of where it is, so each | |||
time a function completes, the program picks up where it left off in | time a function completes, the program picks up where it left off in | ||
the function that called it. When it gets to the end of the program, | the function that called it. When it gets to the end of the program, | ||
it terminates. | it terminates. | ||
What’s the moral of this sordid tale? When you read a program, you | |||
don’t always want to read from top to bottom. Sometimes it makes | don’t always want to read from top to bottom. Sometimes it makes | ||
more sense if you follow the flow of execution. | more sense if you follow the flow of execution. | ||
=== 3.8  Parameters and arguments === | |||
example, when you call | |||
Some of the built-in functions we have seen require arguments. For | |||
example, when you call <TT>math.sin</TT> you pass a number | |||
as an argument. Some functions take more than one argument: | as an argument. Some functions take more than one argument: | ||
<TT>math.pow</TT> takes two, the base and the exponent. | |||
variables called | |||
user-defined function that takes an argument: | Inside the function, the arguments are assigned to | ||
variables called '''parameters'''. Here is an example of a | |||
user-defined function that takes an argument: | |||
<PRE CLASS="verbatim">def print_twice(bruce): | |||
print bruce | print bruce | ||
print bruce | print bruce | ||
</PRE> | |||
named | This function assigns the argument to a parameter | ||
the parameter (whatever it is) twice. | named <TT>bruce</TT>. When the function is called, it prints the value of | ||
the parameter (whatever it is) twice. | |||
This function works with any value that can be printed. | |||
<PRE CLASS="verbatim">>>> print_twice('Spam') | |||
Spam | Spam | ||
Spam | Spam | ||
| Line 229: | Line 361: | ||
3.14159265359 | 3.14159265359 | ||
3.14159265359 | 3.14159265359 | ||
</PRE> | |||
The same rules of composition that apply to built-in functions also | |||
apply to user-defined functions, so we can use any kind of expression | apply to user-defined functions, so we can use any kind of expression | ||
as an argument for | as an argument for <CODE>print_twice</CODE>: | ||
<PRE CLASS="verbatim">>>> print_twice('Spam '*4) | |||
Spam Spam Spam Spam | Spam Spam Spam Spam | ||
Spam Spam Spam Spam | Spam Spam Spam Spam | ||
| Line 237: | Line 372: | ||
-1.0 | -1.0 | ||
-1.0 | -1.0 | ||
</PRE> | |||
in the examples the expressions | The argument is evaluated before the function is called, so | ||
in the examples the expressions <CODE>'Spam '*4</CODE> and | |||
<TT>math.cos(math.pi)</TT> are only evaluated once. | |||
You can also use a variable as an argument: | |||
<PRE CLASS="verbatim">>>> michael = 'Eric, the half a bee.' | |||
>>> print_twice(michael) | >>> print_twice(michael) | ||
Eric, the half a bee. | Eric, the half a bee. | ||
Eric, the half a bee. | Eric, the half a bee. | ||
</PRE> | |||
nothing to do with the name of the parameter ( | The name of the variable we pass as an argument (<TT>michael</TT>) has | ||
nothing to do with the name of the parameter (<TT>bruce</TT>). It | |||
doesn’t matter what the value was called back home (in the caller); | doesn’t matter what the value was called back home (in the caller); | ||
here in | here in <CODE>print_twice</CODE>, we call everybody <TT>bruce</TT>. | ||
=== 3.9  Variables and parameters are local === | |||
When you create a variable inside a function, it is '''local''', | |||
which means that it only | which means that it only | ||
exists inside the function. For example: | exists inside the function. For example: | ||
<PRE CLASS="verbatim">def cat_twice(part1, part2): | |||
cat = part1 + part2 | cat = part1 + part2 | ||
print_twice(cat) | print_twice(cat) | ||
</PRE> | |||
the result twice. Here is an example that uses it: | This function takes two arguments, concatenates them, and prints | ||
the result twice. Here is an example that uses it: | |||
<PRE CLASS="verbatim">>>> line1 = 'Bing tiddle ' | |||
>>> line2 = 'tiddle bang.' | >>> line2 = 'tiddle bang.' | ||
>>> cat_twice(line1, line2) | >>> cat_twice(line1, line2) | ||
Bing tiddle tiddle bang. | Bing tiddle tiddle bang. | ||
Bing tiddle tiddle bang. | Bing tiddle tiddle bang. | ||
</PRE> | |||
is destroyed. If we try to print it, we get an exception: | When <CODE>cat_twice</CODE> terminates, the variable <TT>cat</TT> | ||
is destroyed. If we try to print it, we get an exception: | |||
<PRE CLASS="verbatim">>>> print cat | |||
NameError: name 'cat' is not defined | NameError: name 'cat' is not defined | ||
</PRE> | |||
For example, outside | Parameters are also local. | ||
such thing as | For example, outside <CODE>print_twice</CODE>, there is no | ||
such thing as <TT>bruce</TT>. | |||
=== 3.10  Stack diagrams === | |||
useful to draw a | |||
To keep track of which variables can be used where, it is sometimes | |||
useful to draw a '''stack diagram'''. Like state diagrams, stack | |||
diagrams show the value of each variable, but they also show the | diagrams show the value of each variable, but they also show the | ||
function each variable belongs to. | function each variable belongs to. | ||
Each function is represented by a '''frame'''. A frame is a box | |||
with the name of a function | with the name of a function | ||
beside it and the parameters and variables of the function inside it. | beside it and the parameters and variables of the function inside it. | ||
The stack diagram for the | The stack diagram for the | ||
previous example looks like this: | previous example looks like this: | ||
called which, and so on. In this example, | <DIV CLASS="center"><IMG SRC="book004.png"></DIV> | ||
was called by | The frames are arranged in a stack that indicates which function | ||
called which, and so on. In this example, <CODE>print_twice</CODE> | |||
was called by <CODE>cat_twice</CODE>, and <CODE>cat_twice</CODE> was called by | |||
<CODE>__main__</CODE>, which is a special name for the topmost frame. When | |||
you create a variable outside of any function, it belongs to | you create a variable outside of any function, it belongs to | ||
<CODE>__main__</CODE>. | |||
argument. So, | |||
Each parameter refers to the same value as its corresponding | |||
and | argument. So, <TT>part1</TT> has the same value as | ||
<TT>line1</TT>, <TT>part2</TT> has the same value as <TT>line2</TT>, | |||
and <TT>bruce</TT> has the same value as <TT>cat</TT>. | |||
If an error occurs during a function call, Python prints the | |||
name of the function, and the name of the function that called | name of the function, and the name of the function that called | ||
it, and the name of the function that called | it, and the name of the function that called ''that'', all the | ||
way back to | way back to <CODE>__main__</CODE>. | ||
For example, if you try to access <TT>cat</TT> from within | |||
<CODE>print_twice</CODE>, you get a <TT>NameError</TT>: | |||
<PRE CLASS="verbatim">Traceback (innermost last): | |||
File "test.py", line 13, in __main__ | File "test.py", line 13, in __main__ | ||
cat_twice(line1, line2) | cat_twice(line1, line2) | ||
| Line 297: | Line 471: | ||
print cat | print cat | ||
NameError: name 'cat' is not defined | NameError: name 'cat' is not defined | ||
</PRE> | |||
This list of functions is called a '''traceback'''. It tells you what | |||
program file the error occurred in, and what line, and what functions | program file the error occurred in, and what line, and what functions | ||
were executing at the time. It also shows the line of code that | were executing at the time. It also shows the line of code that | ||
caused the error. | caused the error. | ||
The order of the functions in the traceback is the same as the | |||
order of the frames in the stack diagram. The function that is | order of the frames in the stack diagram. The function that is | ||
currently running is at the bottom. | currently running is at the bottom. | ||
=== 3.11  Fruitful functions and void functions === | |||
results; for lack of a better name, I call them | |||
functions | |||
action but don’t return a value. They are called | |||
functions | |||
Some of the functions we are using, such as the math functions, yield | |||
results; for lack of a better name, I call them '''fruitful | |||
functions'''. Other functions, like <CODE>print_twice</CODE>, perform an | |||
action but don’t return a value. They are called '''void | |||
functions'''. | |||
When you call a fruitful function, you almost always | |||
want to do something with the result; for example, you might | want to do something with the result; for example, you might | ||
assign it to a variable or use it as part of an expression: | assign it to a variable or use it as part of an expression: | ||
<PRE CLASS="verbatim">x = math.cos(radians) | |||
golden = (math.sqrt(5) + 1) / 2 | golden = (math.sqrt(5) + 1) / 2 | ||
</PRE> | |||
the result: | When you call a function in interactive mode, Python displays | ||
the result: | |||
<PRE CLASS="verbatim">>>> math.sqrt(5) | |||
2.2360679774997898 | 2.2360679774997898 | ||
</PRE> | |||
the return value is lost forever! | But in a script, if you call a fruitful function all by itself, | ||
the return value is lost forever! | |||
or display the result, it is not very useful. | <PRE CLASS="verbatim">math.sqrt(5) | ||
</PRE> | |||
This script computes the square root of 5, but since it doesn’t store | |||
or display the result, it is not very useful. | |||
Void functions might display something on the screen or have some | |||
other effect, but they don’t have a return value. If you try to | other effect, but they don’t have a return value. If you try to | ||
assign the result to a variable, you get a special value called | assign the result to a variable, you get a special value called | ||
<TT>None</TT>. | |||
<PRE CLASS="verbatim">>>> result = print_twice('Bing') | |||
Bing | Bing | ||
Bing | Bing | ||
>>> print result | >>> print result | ||
None | None | ||
</PRE> | |||
It is a special value that has its own type: | The value <TT>None</TT> is not the same as the string <CODE>'None'</CODE>. | ||
It is a special value that has its own type: | |||
<PRE CLASS="verbatim">>>> print type(None) | |||
<type 'NoneType'> | <type 'NoneType'> | ||
</PRE> | |||
writing fruitful functions in a few chapters. | The functions we have written so far are all void. We will start | ||
writing fruitful functions in a few chapters. | |||
a program into functions. There are several reasons: | === 3.12  Why functions? === | ||
of statements, which makes your program easier to read and debug. | |||
It may not be clear why it is worth the trouble to divide | |||
a program into functions. There are several reasons: | |||
*Creating a new function gives you an opportunity to name a group | |||
of statements, which makes your program easier to read and debug. | |||
*Functions can make a program smaller by eliminating repetitive | |||
code. Later, if you make a change, you only have | code. Later, if you make a change, you only have | ||
to make it in one place. | to make it in one place. | ||
parts one at a time and then assemble them into a working whole. | |||
Once you write and debug one, you can reuse it. | *Dividing a long program into functions allows you to debug the | ||
parts one at a time and then assemble them into a working whole. | |||
*Well-designed functions are often useful for many programs. | |||
Once you write and debug one, you can reuse it. | |||
=== 3.13  Debugging === | |||
If you are using a text editor to write your scripts, you might | |||
run into problems with spaces and tabs. The best way to avoid | run into problems with spaces and tabs. The best way to avoid | ||
these problems is to use spaces exclusively (no tabs). Most text | these problems is to use spaces exclusively (no tabs). Most text | ||
editors that know about Python do this by default, but some | editors that know about Python do this by default, but some | ||
don’t. | don’t. | ||
Tabs and spaces are usually invisible, which makes them | |||
hard to debug, so try to find an editor that manages indentation | hard to debug, so try to find an editor that manages indentation | ||
for you. | for you. | ||
Also, don’t forget to save your program before you run it. Some | |||
development environments do this automatically, but some don’t. | development environments do this automatically, but some don’t. | ||
In that case the program you are looking at in the text editor | In that case the program you are looking at in the text editor | ||
is not the same as the program you are running. | is not the same as the program you are running. | ||
incorrect, program over and over! | |||
If you’re not sure, put something like | Debugging can take a long time if you keep running the same, | ||
incorrect, program over and over! | |||
Make sure that the code you are looking at is the code you are running. | |||
If you’re not sure, put something like <CODE>print 'hello'</CODE> at the | |||
beginning of the program and run it again. If you don’t see | beginning of the program and run it again. If you don’t see | ||
<CODE>hello</CODE>, you’re not running the right program! | |||
=== 3.14  Glossary === | |||
<DL CLASS="description"><DT CLASS="dt-description">'''function:'''</DT><DD CLASS="dd-description"> A named sequence of statements that performs some | |||
useful operation. Functions may or may not take arguments and may or | useful operation. Functions may or may not take arguments and may or | ||
may not produce a result. | may not produce a result. | ||
</DD><DT CLASS="dt-description">'''function definition:'''</DT><DD CLASS="dd-description"> A statement that creates a new function, | |||
specifying its name, parameters, and the statements it executes. | specifying its name, parameters, and the statements it executes. | ||
</DD><DT CLASS="dt-description">'''function object:'''</DT><DD CLASS="dd-description"> A value created by a function definition. | |||
The name of the function is a variable that refers to a function | The name of the function is a variable that refers to a function | ||
object. | object. | ||
</DD><DT CLASS="dt-description">'''header:'''</DT><DD CLASS="dd-description"> The first line of a function definition. | |||
</DD><DT CLASS="dt-description">'''body:'''</DT><DD CLASS="dd-description"> The sequence of statements inside a function definition. | |||
</DD><DT CLASS="dt-description">'''parameter:'''</DT><DD CLASS="dd-description"> A name used inside a function to refer to the value | |||
passed as an argument. | passed as an argument. | ||
</DD><DT CLASS="dt-description">'''function call:'''</DT><DD CLASS="dd-description"> A statement that executes a function. It | |||
consists of the function name followed by an argument list. | consists of the function name followed by an argument list. | ||
</DD><DT CLASS="dt-description">'''argument:'''</DT><DD CLASS="dd-description"> A value provided to a function when the function is called. | |||
This value is assigned to the corresponding parameter in the function. | This value is assigned to the corresponding parameter in the function. | ||
</DD><DT CLASS="dt-description">'''local variable:'''</DT><DD CLASS="dd-description"> A variable defined inside a function. A local | |||
variable can only be used inside its function. | variable can only be used inside its function. | ||
</DD><DT CLASS="dt-description">'''return value:'''</DT><DD CLASS="dd-description"> The result of a function. If a function call | |||
is used as an expression, the return value is the value of | is used as an expression, the return value is the value of | ||
the expression. | the expression. | ||
</DD><DT CLASS="dt-description">'''fruitful function:'''</DT><DD CLASS="dd-description"> A function that returns a value. | |||
</DD><DT CLASS="dt-description">'''void function:'''</DT><DD CLASS="dd-description"> A function that doesn’t return a value. | |||
</DD><DT CLASS="dt-description">'''module:'''</DT><DD CLASS="dd-description"> A file that contains a | |||
collection of related functions and other definitions. | collection of related functions and other definitions. | ||
</DD><DT CLASS="dt-description">'''import statement:'''</DT><DD CLASS="dd-description"> A statement that reads a module file and creates | |||
a module object. | a module object. | ||
</DD><DT CLASS="dt-description">'''module object:'''</DT><DD CLASS="dd-description"> A value created by an <TT>import</TT> statement | |||
that provides access to the values defined in a module. | that provides access to the values defined in a module. | ||
</DD><DT CLASS="dt-description">'''dot notation:'''</DT><DD CLASS="dd-description"> The syntax for calling a function in another | |||
module by specifying the module name followed by a dot (period) and | module by specifying the module name followed by a dot (period) and | ||
the function name. | the function name. | ||
</DD><DT CLASS="dt-description">'''composition:'''</DT><DD CLASS="dd-description"> Using an expression as part of a larger expression, | |||
or a statement as part of a larger statement. | or a statement as part of a larger statement. | ||
</DD><DT CLASS="dt-description">'''flow of execution:'''</DT><DD CLASS="dd-description"> The order in which statements are executed during | |||
a program run. | a program run. | ||
</DD><DT CLASS="dt-description">'''stack diagram:'''</DT><DD CLASS="dd-description"> A graphical representation of a stack of functions, | |||
their variables, and the values they refer to. | their variables, and the values they refer to. | ||
</DD><DT CLASS="dt-description">'''frame:'''</DT><DD CLASS="dd-description"> A box in a stack diagram that represents a function call. | |||
It contains the local variables and parameters of the function. | It contains the local variables and parameters of the function. | ||
</DD><DT CLASS="dt-description">'''traceback:'''</DT><DD CLASS="dd-description"> A list of the functions that are executing, | |||
printed when an exception occurs. | printed when an exception occurs. | ||
</DD></DL>=== 3.15  Exercises === | |||
returns the length of a string, so the value of | <DIV CLASS="theorem">'''Exercise 3'''   | ||
named | '' | ||
'' | |||
''Python provides a built-in function called ''''<TT>len</TT>'''' that | |||
returns the length of a string, so the value of ''<CODE>''len('allen')''</CODE>'' is 5.'' | |||
''Write a function named ''<CODE>''right_justify''</CODE>'' that takes a string | |||
named ''''<TT>s</TT>'''' as a parameter and prints the string with enough | |||
leading spaces so that the last letter of the string is in column 70 | leading spaces so that the last letter of the string is in column 70 | ||
of the display. | of the display.'' | ||
<PRE CLASS="verbatim">''>>> right_justify('allen') | |||
allen | allen | ||
''</PRE></DIV><DIV CLASS="theorem">'''Exercise 4'''  '' | |||
'''' | |||
'' | |||
or pass as an argument. For example, | ''A function object is a value you can assign to a variable | ||
that takes a function object as an argument and calls it twice: | or pass as an argument. For example, ''<CODE>''do_twice''</CODE>'' is a function | ||
that takes a function object as an argument and calls it twice:'' | |||
<PRE CLASS="verbatim">''def do_twice(f): | |||
f() | f() | ||
f() | f() | ||
''</PRE> | |||
named | ''Here’s an example that uses ''<CODE>''do_twice''</CODE>'' to call a function | ||
named ''<CODE>''print_spam''</CODE>'' twice.'' | |||
<PRE CLASS="verbatim">''def print_spam(): | |||
print 'spam' | print 'spam' | ||
do_twice(print_spam) | do_twice(print_spam) | ||
''</PRE> | |||
*''Type this example into a script and test it.'' | |||
*''Modify ''<CODE>''do_twice''</CODE>'' so that it takes two arguments, a | |||
function object and a value, and calls the function twice, | function object and a value, and calls the function twice, | ||
passing the value as an argument. | passing the value as an argument.'' | ||
it twice. | *''Write a more general version of ''<CODE>''print_spam''</CODE>'', called | ||
''<CODE>''print_twice''</CODE>'', that takes a string as a parameter and prints | |||
it twice.'' | |||
*''Use the modified version of ''<CODE>''do_twice''</CODE>'' to call | |||
''<CODE>''print_twice''</CODE>'' twice, passing ''<CODE>'''spam'''</CODE>'' as an argument.'' | |||
*''Define a new function called | |||
''<CODE>''do_four''</CODE>'' that takes a function object and a value | |||
and calls the function four times, passing the value | and calls the function four times, passing the value | ||
as a parameter. There should be only | as a parameter. There should be only | ||
two statements in the body of this function, not four. | two statements in the body of this function, not four.'' | ||
This exercise | |||
''You can see my solution at ''''<TT>thinkpython.com/code/do_four.py</TT>''''.'' | |||
</DIV><DIV CLASS="theorem">'''Exercise 5'''  '' | |||
This exercise''<SUP>''2''</SUP>'' can be | |||
done using only the statements and other features we have learned so | done using only the statements and other features we have learned so | ||
far. | far. '' | ||
following: | |||
*''Write a function that draws a grid like the | |||
following:''<PRE CLASS="verbatim">''+ - - - - + - - - - + | |||
| | | | | | | | ||
| | | | | | | | ||
| Line 442: | Line 699: | ||
| | | | | | | | ||
+ - - - - + - - - - + | + - - - - + - - - - + | ||
''</PRE> | |||
a comma-separated sequence: | ''''Hint: to print more than one value on a line, you can print | ||
a comma-separated sequence:'''' | |||
so the value printed next appears on the same line. | <PRE CLASS="verbatim">''''print '+', '-' | ||
''''</PRE> | |||
''''If the sequence ends with a comma, Python leaves the line unfinished, | |||
so the value printed next appears on the same line.'''' | |||
<PRE CLASS="verbatim">''''print '+', | |||
print '-' | print '-' | ||
''''</PRE> | |||
goes to the next line. | ''''The output of these statements is ''''<CODE>'''''+ -'''''</CODE>''''.'''' | ||
with four rows and four columns. | |||
''''A ''''''''<TT>print</TT>'''''''' statement all by itself ends the current line and | |||
goes to the next line.'''' | |||
*''''Use the previous function to draw a similar grid | |||
with four rows and four columns.'''' | |||
''You can see my solution at ''''<TT>thinkpython.com/code/grid.py</TT>''''.'' | |||
</DIV><HR CLASS="footnoterule"><DL CLASS="thefootnotes"><DT CLASS="dt-thefootnotes"> | |||
1</DT><DD CLASS="dd-thefootnotes">We will see exceptions to this rule | |||
later. | later. | ||
</DD><DT CLASS="dt-thefootnotes">2</DT><DD CLASS="dd-thefootnotes">Based on an exercise in Oualline, ''Practical C Programming, Third Edition'', O’Reilly (1997) | |||
</DD></DL> | |||
<HR> | <HR> | ||
<IMG SRC="previous_motif.gif" ALT="Previous"> | |||
<IMG SRC="contents_motif.gif" ALT="Up"> | |||
<IMG SRC="next_motif.gif" ALT="Next"> | |||
Latest revision as of 20:09, 18 May 2009
?
Chapter 3 Functions
3.1 Function calls
In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name. We have already seen one example of a function call:
>>> type(32) <type 'int'>
The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.
It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value.
3.2 Type conversion functions
Python provides built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:
>>> int('32')
32
>>> int('Hello')
ValueError: invalid literal for int(): Hello
int can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:
>>> int(3.99999) 3 >>> int(-2.3) -2
float converts integers and strings to floating-point numbers:
>>> float(32)
32.0
>>> float('3.14159')
3.14159
Finally, str converts its argument to a string:
>>> str(32) '32' >>> str(3.14159) '3.14159'
=== 3.3 Math functions ===
Python has a math module that provides most of the familiar mathematical functions. A module is a file that contains a collection of related functions.
Before we can use the module, we have to import it:
>>> import math
This statement creates a module object named math. If you print the module object, you get some information about it:
>>> print math <module 'math' from '/usr/lib/python2.5/lib-dynload/math.so'>
The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.
>>> ratio = signal_power / noise_power >>> decibels = 10 * math.log10(ratio) >>> radians = 0.7 >>> height = math.sin(radians)
The first example computes the logarithm base 10 of the signal-to-noise ratio. The math module also provides a function called log that computes logarithms base e.
The second example finds the sine of radians. The name of the
variable is a hint that sin and the other trigonometric
functions (cos, tan, etc.) take arguments in radians. To
convert from degrees to radians, divide by 360 and multiply by 2
π:
>>> degrees = 45 >>> radians = degrees / 360.0 * 2 * math.pi >>> math.sin(radians) 0.707106781187
The expression math.pi gets the variable pi from the math module. The value of this variable is an approximation of π, accurate to about 15 digits.
If you know your trigonometry, you can check the previous result by comparing it to the square root of two divided by two:
>>> math.sqrt(2) / 2.0 0.707106781187
=== 3.4 Composition ===
So far, we have looked at the elements of a program—variables,
expressions, and statements—in isolation, without talking about how to
combine them.
One of the most useful features of programming languages is their ability to take small building blocks and compose them. For example, the argument of a function can be any kind of expression, including arithmetic operators:
x = math.sin(degrees / 360.0 * 2 * math.pi)
And even function calls:
x = math.exp(math.log(x+1))
Almost anywhere you can put a value, you can put an arbitrary expression, with one exception: the left side of an assignment statement has to be a variable name. Any other expression on the left side is a syntax error1.
>>> minutes = hours * 60 # right >>> hours * 60 = minutes # wrong! SyntaxError: can't assign to operator
3.5 Adding new functions
So far, we have only been using the functions that come with Python, but it is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called.
Here is an example:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
def is a keyword that indicates that this is a function
definition. The name of the function is print_lyrics. The
rules for function names are the same as for variable names: letters,
numbers and some punctuation marks are legal, but the first character
can’t be a number. You can’t use a keyword as the name of a function,
and you should avoid having a variable and a function with the same
name.
The empty parentheses after the name indicate that this function doesn’t take any arguments.
The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, the indentation is always four spaces (see Section 3.13). The body can contain any number of statements.
The strings in the print statements are enclosed in double quotes. Single quotes and double quotes do the same thing; most people use single quotes except in cases like this where a single quote (which is also an apostrophe) appears in the string.
If you type a function definition in interactive mode, the interpreter prints ellipses (...) to let you know that the definition isn’t complete:
>>> def print_lyrics(): ... print "I'm a lumberjack, and I'm okay." ... print "I sleep all night and I work all day." ...
To end the function, you have to enter an empty line (this is not necessary in a script).
Defining a function creates a variable with the same name.
>>> print print_lyrics <function print_lyrics at 0xb7e99e9c> >>> print type(print_lyrics) <type 'function'>
The value of print_lyrics is a function object, which
has type 'function'.
The syntax for calling the new function is the same as
for built-in functions:
>>> print_lyrics() I'm a lumberjack, and I'm okay. I sleep all night and I work all day.
Once you have defined a function, you can use it inside another
function. For example, to repeat the previous refrain, we could write
a function called repeat_lyrics:
def repeat_lyrics():
print_lyrics()
print_lyrics()
And then call repeat_lyrics:
>>> repeat_lyrics() I'm a lumberjack, and I'm okay. I sleep all night and I work all day. I'm a lumberjack, and I'm okay. I sleep all night and I work all day.
But that’s not really how the song goes.
3.6 Definitions and uses
Pulling together the code fragments from the previous section, the whole program looks like this:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
def repeat_lyrics():
print_lyrics()
print_lyrics()
repeat_lyrics()
This program contains two function definitions: print_lyrics and
repeat_lyrics. Function definitions get executed just like other
statements, but the effect is to create function objects. The statements
inside the function do not get executed until the function is called, and
the function definition generates no output.
As you might expect, you have to create a function before you can execute it. In other words, the function definition has to be executed before the first time it is called.
Move the last line of this program to the top, so the function call appears before the definitions. Run the program and see what error message you get.
Move the function call back to the bottom
and move the definition of print_lyrics after the definition of
repeat_lyrics. What happens when you run this program?
=== 3.7 Flow of execution ===
In order to ensure that a function is defined before its first use,
you have to know the order in which statements are executed, which is
called the flow of execution.
Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom.
Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.
A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, executes all the statements there, and then comes back to pick up where it left off.
That sounds simple enough, until you remember that one function can call another. While in the middle of one function, the program might have to execute the statements in another function. But while executing that new function, the program might have to execute yet another function!
Fortunately, Python is good at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.
What’s the moral of this sordid tale? When you read a program, you don’t always want to read from top to bottom. Sometimes it makes more sense if you follow the flow of execution.
3.8 Parameters and arguments
Some of the built-in functions we have seen require arguments. For example, when you call math.sin you pass a number as an argument. Some functions take more than one argument: math.pow takes two, the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Here is an example of a user-defined function that takes an argument:
def print_twice(bruce):
print bruce
print bruce
This function assigns the argument to a parameter named bruce. When the function is called, it prints the value of the parameter (whatever it is) twice.
This function works with any value that can be printed.
>>> print_twice('Spam')
Spam
Spam
>>> print_twice(17)
17
17
>>> print_twice(math.pi)
3.14159265359
3.14159265359
The same rules of composition that apply to built-in functions also
apply to user-defined functions, so we can use any kind of expression
as an argument for print_twice:
>>> print_twice('Spam '*4)
Spam Spam Spam Spam
Spam Spam Spam Spam
>>> print_twice(math.cos(math.pi))
-1.0
-1.0
The argument is evaluated before the function is called, so
in the examples the expressions 'Spam '*4 and
math.cos(math.pi) are only evaluated once.
You can also use a variable as an argument:
>>> michael = 'Eric, the half a bee.' >>> print_twice(michael) Eric, the half a bee. Eric, the half a bee.
The name of the variable we pass as an argument (michael) has
nothing to do with the name of the parameter (bruce). It
doesn’t matter what the value was called back home (in the caller);
here in print_twice, we call everybody bruce.
3.9 Variables and parameters are local
When you create a variable inside a function, it is local, which means that it only exists inside the function. For example:
def cat_twice(part1, part2):
cat = part1 + part2
print_twice(cat)
This function takes two arguments, concatenates them, and prints the result twice. Here is an example that uses it:
>>> line1 = 'Bing tiddle ' >>> line2 = 'tiddle bang.' >>> cat_twice(line1, line2) Bing tiddle tiddle bang. Bing tiddle tiddle bang.
When cat_twice terminates, the variable cat
is destroyed. If we try to print it, we get an exception:
>>> print cat NameError: name 'cat' is not defined
Parameters are also local.
For example, outside print_twice, there is no
such thing as bruce.
3.10 Stack diagrams
To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram. Like state diagrams, stack diagrams show the value of each variable, but they also show the function each variable belongs to.
Each function is represented by a frame. A frame is a box
with the name of a function
beside it and the parameters and variables of the function inside it.
The stack diagram for the
previous example looks like this:
The frames are arranged in a stack that indicates which function
called which, and so on. In this example, print_twice
was called by cat_twice, and cat_twice was called by
__main__, which is a special name for the topmost frame. When
you create a variable outside of any function, it belongs to
__main__.
Each parameter refers to the same value as its corresponding argument. So, part1 has the same value as line1, part2 has the same value as line2, and bruce has the same value as cat.
If an error occurs during a function call, Python prints the
name of the function, and the name of the function that called
it, and the name of the function that called that, all the
way back to __main__.
For example, if you try to access cat from within
print_twice, you get a NameError:
Traceback (innermost last):
File "test.py", line 13, in __main__
cat_twice(line1, line2)
File "test.py", line 5, in cat_twice
print_twice(cat)
File "test.py", line 9, in print_twice
print cat
NameError: name 'cat' is not defined
This list of functions is called a traceback. It tells you what program file the error occurred in, and what line, and what functions were executing at the time. It also shows the line of code that caused the error.
The order of the functions in the traceback is the same as the order of the frames in the stack diagram. The function that is currently running is at the bottom.
3.11 Fruitful functions and void functions
Some of the functions we are using, such as the math functions, yield
results; for lack of a better name, I call them fruitful
functions. Other functions, like print_twice, perform an
action but don’t return a value. They are called void
functions.
When you call a fruitful function, you almost always want to do something with the result; for example, you might assign it to a variable or use it as part of an expression:
x = math.cos(radians) golden = (math.sqrt(5) + 1) / 2
When you call a function in interactive mode, Python displays the result:
>>> math.sqrt(5) 2.2360679774997898
But in a script, if you call a fruitful function all by itself, the return value is lost forever!
math.sqrt(5)
This script computes the square root of 5, but since it doesn’t store or display the result, it is not very useful.
Void functions might display something on the screen or have some
other effect, but they don’t have a return value. If you try to
assign the result to a variable, you get a special value called
None.
>>> result = print_twice('Bing')
Bing
Bing
>>> print result
None
The value None is not the same as the string 'None'.
It is a special value that has its own type:
>>> print type(None) <type 'NoneType'>
The functions we have written so far are all void. We will start writing fruitful functions in a few chapters.
3.12 Why functions?
It may not be clear why it is worth the trouble to divide a program into functions. There are several reasons:
- Creating a new function gives you an opportunity to name a group
of statements, which makes your program easier to read and debug.
- Functions can make a program smaller by eliminating repetitive
code. Later, if you make a change, you only have to make it in one place.
- Dividing a long program into functions allows you to debug the
parts one at a time and then assemble them into a working whole.
- Well-designed functions are often useful for many programs.
Once you write and debug one, you can reuse it.
3.13 Debugging
If you are using a text editor to write your scripts, you might run into problems with spaces and tabs. The best way to avoid these problems is to use spaces exclusively (no tabs). Most text editors that know about Python do this by default, but some don’t.
Tabs and spaces are usually invisible, which makes them hard to debug, so try to find an editor that manages indentation for you.
Also, don’t forget to save your program before you run it. Some development environments do this automatically, but some don’t. In that case the program you are looking at in the text editor is not the same as the program you are running.
Debugging can take a long time if you keep running the same, incorrect, program over and over!
Make sure that the code you are looking at is the code you are running.
If you’re not sure, put something like print 'hello' at the
beginning of the program and run it again. If you don’t see
hello, you’re not running the right program!
3.14 Glossary
- function:
- A named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result.
- function definition:
- A statement that creates a new function, specifying its name, parameters, and the statements it executes.
- function object:
- A value created by a function definition. The name of the function is a variable that refers to a function object.
- header:
- The first line of a function definition.
- body:
- The sequence of statements inside a function definition.
- parameter:
- A name used inside a function to refer to the value passed as an argument.
- function call:
- A statement that executes a function. It consists of the function name followed by an argument list.
- argument:
- A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function.
- local variable:
- A variable defined inside a function. A local variable can only be used inside its function.
- return value:
- The result of a function. If a function call is used as an expression, the return value is the value of the expression.
- fruitful function:
- A function that returns a value.
- void function:
- A function that doesn’t return a value.
- module:
- A file that contains a collection of related functions and other definitions.
- import statement:
- A statement that reads a module file and creates a module object.
- module object:
- A value created by an import statement that provides access to the values defined in a module.
- dot notation:
- The syntax for calling a function in another module by specifying the module name followed by a dot (period) and the function name.
- composition:
- Using an expression as part of a larger expression, or a statement as part of a larger statement.
- flow of execution:
- The order in which statements are executed during a program run.
- stack diagram:
- A graphical representation of a stack of functions, their variables, and the values they refer to.
- frame:
- A box in a stack diagram that represents a function call. It contains the local variables and parameters of the function.
- traceback:
- A list of the functions that are executing, printed when an exception occurs.
=== 3.15 Exercises ===
Python provides a built-in function called 'len' that
returns the length of a string, so the value of len('allen') is 5.
Write a function named right_justify that takes a string
named 's' as a parameter and prints the string with enough
leading spaces so that the last letter of the string is in column 70
of the display.
''>>> right_justify('allen')
allen
'''
A function object is a value you can assign to a variable
or pass as an argument. For example, do_twice is a function
that takes a function object as an argument and calls it twice:
''def do_twice(f):
f()
f()
''
Here’s an example that uses do_twice to call a function
named print_spam twice.
''def print_spam():
print 'spam'
do_twice(print_spam)
''
- Type this example into a script and test it.
- Modify
do_twiceso that it takes two arguments, a
function object and a value, and calls the function twice, passing the value as an argument.
- Write a more general version of
print_spam, called
print_twice, that takes a string as a parameter and prints
it twice.
- Use the modified version of
do_twiceto call
print_twice twice, passing spam as an argument.
- Define a new function called
do_four that takes a function object and a value
and calls the function four times, passing the value
as a parameter. There should be only
two statements in the body of this function, not four.
You can see my solution at 'thinkpython.com/code/do_four.py'.
This exercise2 can be done using only the statements and other features we have learned so far.
- Write a function that draws a grid like the
''+ - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + ''
'Hint: to print more than one value on a line, you can print a comma-separated sequence:'
''''print '+', '-' ''''
'If the sequence ends with a comma, Python leaves the line unfinished, so the value printed next appears on the same line.'
''''print '+', print '-' ''''
'The output of these statements is '+ -'.'
'A '''print''' statement all by itself ends the current line and goes to the next line.'
- 'Use the previous function to draw a similar grid
with four rows and four columns.'
You can see my solution at 'thinkpython.com/code/grid.py'.
- 1
- We will see exceptions to this rule later.
- 2
- Based on an exercise in Oualline, Practical C Programming, Third Edition, O’Reilly (1997)
<IMG SRC="previous_motif.gif" ALT="Previous"> <IMG SRC="contents_motif.gif" ALT="Up"> <IMG SRC="next_motif.gif" ALT="Next">