Книга: Practical Programming, Fourth Edition
Назад: Defining Custom Functions
Дальше: Tracing Function Calls in th e Memory Model

Using Local Variables for Temporary Storage

Some computations are complex, and breaking them down into separate steps can lead to clearer code. In the following example, let’s break down the evaluation of the quadratic polynomial ax2+ bx + c into several steps. Notice that all the statements inside the function are indented the same number of spaces to be aligned with each other. You can type this example into an editor first (without the leading >>> and ...) and then paste it into the Python shell. That makes fixing mistakes much easier:

 >>>​​ ​​def​​ ​​quadratic(a,​​ ​​b,​​ ​​c,​​ ​​x):
 ...​​ ​​first​​ ​​=​​ ​​a​​ ​​*​​ ​​x​​ ​​**​​ ​​2
 ...​​ ​​second​​ ​​=​​ ​​b​​ ​​*​​ ​​x
 ...​​ ​​third​​ ​​=​​ ​​c
 ...​​ ​​return​​ ​​first​​ ​​+​​ ​​second​​ ​​+​​ ​​third
 ...
 >>>​​ ​​quadratic(2,​​ ​​3,​​ ​​4,​​ ​​0.5)
 6.0
 >>>​​ ​​quadratic(2,​​ ​​3,​​ ​​4,​​ ​​1.5)
 13.0

A local variable is used within a function definition

Variables like first, second, and third that are created within a function are referred to as local variables. Local variables get created each time that function is called, and they are erased when the function returns. Because they only exist when the function is being executed, they can’t be used outside of the function. Trying to access a local variable from outside the function is an error, just like trying to access a variable that has never been defined is an error:

 >>>​​ ​​quadratic(2,​​ ​​3,​​ ​​4,​​ ​​1.3)
 11.280000000000001
 >>>​​ ​​first
 Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
  first
 NameError: name 'first' is not defined. Did you mean: 'list'?

A function’s parameters are also local variables (assigned the argument values), so you get the same error if you try to use them outside of a function definition:

 >>>​​ ​​a
 Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
  a
 NameError: name 'a' is not defined

The part of a program in which a variable can be used is called the variable’s scope. The scope of a local variable is from the line on which it is defined up until the end of the function.

As you might expect, if a function is defined to take a certain number of parameters, a call to that function must have the same number of arguments:

 >>>​​ ​​quadratic(1,​​ ​​2,​​ ​​3)
 Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
  quadratic(1, 2, 3)
  ~~~~~~~~~^^^^^^^^^
 TypeError: quadratic() missing 1 required positional argument: 'x'

A “positional argument” mentioned above is an argument that is referred to by its position on the list (such as “the first” or “the second”), as opposed to keyword arguments known by their names (see ). Remember that you can use the built-in function help to find out information about a function’s parameters.

Назад: Defining Custom Functions
Дальше: Tracing Function Calls in th e Memory Model