One interesting consequence of the Python world-view is that a
function is an object of the class
callable. The common feature that all
callables share is that they have a very simple
interface: they can be called. Other
include the built-in functions, generator functions (which have the
statement instead of the
statement) and things called lambdas.
Sometimes we don't want to call and evaluate a function. Sometimes
we want to do other things to or with a function. For example, the various
factory functions (
complex) can be used with
isinstance function instead of being called to
create a new object.
isinstance(2,int) has a value of
True. It uses the
but doesn't apply the
A function object is created with the
statement. Primarily, we want to evaluate the function objects we create.
However, because a function is an object, it has attributes, and it can be
manipulated to a limited extent.
From a syntax point of view, a name followed by
a function call. You can think of the
()'s as the "call"
operator: they require evaluation of the arguments, then they apply the
When we use a function name without
()'s, we are
talking about the
function object. There are a
number of manipulations that you might want to do with a function
Call The Function. By far, the most common use for a function object is to call it.
When we follow a function name with
()'s, we are calling
the function: evaluating the arguments, and applying the function.
Calling the function is the most common manipulation.
Alias The Function. This is dangerous, because it can make a program obscure. However,
it can also simplify the evoluation and enhancement of software. Imagine
that the first version of our program had two functions named
definitions might look like the following.
return random.randrange(1,7) + random.randrange(1,7)
When we wanted to expand our program to handle five-dice games, we
realized we could generalize this function.
def rollNDice( n=2 ):
for d in range(n):
t += random.randrange( 1, 7 )
It is important to remove the duplicated algorithm in all three
versions of our dice rolling function. Since
rollDice are just special cases of
rollNDice, we should replace them with something like
return rollNDice( 1 )
This revised definition of
rollDice is really
just an another name for the
ollNDice. Because a
function is an object assigned to a variable, we can have multiple
variables assigned to the function. Here's how we create an alias to a
rollDice = rollNDice
Function alias definitions helps maintaining compatibility between
old and new releases of software. It is not something that should be
done as a general practice; we need to be careful providing multiple
names for a given function. This can be a simplification. It can also be
a big performance improvement for certain types of functions that are
heavily used deep within nested loops.
Function Attributes. A function object has a number of attributes. We can interrogate
those attributes, and to a limited extend, we can change some of these
attributes. For more information, see section 3.2 of the
Python Language Reference and section 18.104.22.168 of
the Python Library Reference.
Docstring from the first line of the function's body.
Function name from the
Name of the module in which the function name was
Tuple with default values to be assigned to each argument that
has a default value. This is a subset of the parameters, starting
with the first parameter that has a default value.
The actual code object that is the suite of statements in the
body of this function.
The dictionary that defines the global namespace for the
module that defines this function. This is
m.__dict__ of the module which defined this
The dictionary that defines the local namespace for the
attributes of this function.
You can set and get your own function attributes, also.