Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Object Method Functions

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:

someObject.aMethod ( argument list )

A single . 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 string and list classes, both of which are special kinds of sequences, have several methods functions that are unique to strings or lists.

For example:

>>> 
"Hi Mom".lower()

'hi mom'

Here, we call the lower method function, which belongs to the string object "Hi Mom".

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. The array module, for example, has an array function that creates array objects. An array 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, also. A file object is created with a built-in function, 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 objects.


 
 
  Published under the terms of the Open Publication License Design by Interspire