Creating Classes, Methods, and Objects


To build an object, you first need a blueprint, or a class. Classes almost always include methods, things that an object can do. You can create a class without any methods, but that wouldn't be much fun.

Introducing the Simple Critter Program

The Simple Critter program includes your first example of a class written in Python. In it, I define an extremely simple type of critter that can only do one thing: say hi. While this kind of critter might be simple, at least it's polite. The results of the program are pictured in Figure 8.4

click to expand
Figure 8.4: When the program invokes the Critter object's talk() method, the critter greets the world.

The program is quite short. Here's the code in its entirety:

 # Simple Critter # Demonstrates a basic class and object # Michael Dawson - 3/23/03 class Critter(object):     """A virtual pet"""     def talk(self):         print "Hi. I'm an instance of class Critter." # main crit = Critter() crit.talk() raw_input("\n\nPress the enter key to exit.") 

Defining a Class

The program starts with a class definition, the blueprint of my first critter. The first line of the definition is the class header:

 class Critter(object): 

I used the keyword class followed by the class name I chose, Critter. You'll notice that my class name begins with a capital letter. Python doesn't require this, but it's the standard convention, so you should begin all your class names with a capital letter.

Next, I told Python to base my class on object, a fundamental, built-in type. You can base a new class on object or any previously defined class, but that's a topic for Chapter 9, "Object-Oriented Programming: The Blackjack Game." In this chapter, I base all of my classes on object.

TRAP

If you're using a version of Python before 2.2, you can't base your classes on object. So, to get the programs in this chapter to run, you'll need to remove (object) from the class headers. My advice though is to use Python 2.2 or later, if at all possible. Toward the end of this chapter, I'll explain exactly what's going on in the evolution of Python classes and objects.

The next line is a docstring, which documents the class. A good docstring describes the kind of objects a class can be used to create. My docstring is pretty straightforward:

     """A virtual pet""" 

Defining a Method

The last part of the class defines a method. It looks very much like a function:

     def talk(self):         print "Hi. I'm an instance of class Critter." 

In fact, you can think of methods as functions associated with an object. (You've already seen this with string and list methods, for example.) The talk() method prints the string "Hi. I'm an instance of class Critter."

You'll notice that talk() has one parameter, self (which it doesn't happen to use). Every method must have a special first parameter, called self by convention, in its parameter list. It provides a way for a method to refer to the object itself. For now, don't worry about self, you'll see it in action a little later in this chapter.

TRAP

If you create an instance method without any parameters, you'll generate an error when you invoke it. Remember, all instance methods must have a special first parameter, called self by convention.

Instantiating an Object

After I wrote my class, instantiating a new object took just one line:

 crit = Critter() 

This line creates a brand-new object of the Critter class and assigns it to the variable crit. Notice the parentheses after the class name Critter in the assignment statement. It's critical to use them if you want to create a new object.

You can assign a newly instantiated object to a variable with any name. The name doesn't have to be based on the class name. However, you should avoid using the same name in lowercase letters as the class name because it could lead to confusion.

Invoking a Method

My new object has a method called talk(). The method is like any other method you've already seen. It's basically a function that belongs to the object. I can invoke this method just like any other, using dot notation:

 crit.talk() 

The line invokes the talk() method of the Critter object assigned to crit. The method simply prints the string "Hi. I'm an instance of class Critter."




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net