We've seen how we can create functions and use those functions in
programs and other functions. Python has a related technique called
methods or method functions.
The functions we've used so far are globally available. A method function,
on the other hand, belongs to an object. The object's class defines what
methods and what properties the object has.
We'll cover method functions in detail, starting in Chapter 21, Classes
. For now, however, some of the Python data
types we're going to introduce in Part II, “Data Structures” will use method
functions. Rather than cover too many details, we'll focus on general
principles of how you use method functions in this section.
The syntax for calling a method function looks like this:
. separates the owning object
someObject) from the method name
aMethod). Some methods have no arguments, others
have complex arguments.
We glanced at a simple example with complex numbers. The complex
conjugate function is actually a method function of the complex number
object. The example is in the section called “Complex Numbers”.
In the next chapter, we'll look at various kinds of sequences.
Python defines some generic method functions that apply to any of the
various classes of sequences. The
list classes, both of which are special kinds of
sequences, have several methods functions that are unique to strings or
Here, we call the
lower method function,
which belongs to the string object
When we describe modules in Part IV, “Components, Modules and Packages”, we'll cover
module functions. These are functions that are imported with the module.
array module, for example, has an
array function that creates
array objects. An
object has several method functions. Additionally, an
array object is a kind of sequence, so it has all
of the methods common to sequences, also.
file objects have an interesting life cycle,
file object is created with a built-in
file. A file object has numerous method
functions, many of which have side-effects of reading from and writing to
external files or devices. We'll cover files in Chapter 19, Files
, listing most of the methods unique to file