Log in / Register
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.3 Calling Methods on Objects

Objects are useful because they allow us to collect related information and group them with behavior that act on this data. These behaviors are called methods in Python. There are two kinds of methods in any object-oriented language: mutator and accessor methods. Accessor methods access the current state of an object but don't change the object. Accessor methods return new object references when called.

x = 'how are you' y = x.upper()


Here, the method upper is called on the object that x refers to. The upper accessor method returns a new object, a str object, that is an upper-cased version of the original string. Note that x is not changed by calling the upper method on it. The upper method is an accessor method. There are many accessor methods available on the str type which you can learn about in the appendices.

Some methods are mutator methods. These methods actually change the existing object. One good example of this is the reverse method on the list type.

myList = [1, 2, 3] myList.reverse()

print(myList) # This prints [3, 2, 1] to the screen

The reverse method mutates the existing object, in this case the list that myList refers to. Once called, a mutator method can't be undone. The change or mutation is permanent until mutated again by some other mutator method.

All classes contain accessor methods. Without accessor methods, the class would be pretty uninteresting. We use accessor methods to retrieve a value that is stored in an object or to retrieve a value that depends on the value stored in an object.

If a class had no accessor methods we could put values in the object but we could never retrieve them.

Some classes have mutator methods and some don't. For instance, the list class has mutator methods, including the reverse method. There are some classes that don't have any mutator methods. For instance, the str class does not have any mutator methods. When a class does not contain any mutator methods, we say that the class is immutable. We can form new values from the data in an immutable class, but once an immutable object is created, it cannot be changed. Other immutable classes include int and float.

1.4 Implementing a Class

Programming in an object-oriented language usually means implementing classes that describe objects which hold information that is needed by the program you are writing. Objects contain data and methods operate on that data. A class is the definition of the data and methods for a specific type of object.

Every class contains one special method called a constructor. The constructor's job is to create an instance of an object by placing references to data within the object itself. For example, consider a class called Dog. A dog has a name, a birthday, and a sound it makes when it barks. When we create a Dog object, we write code like that appearing in Sect. 1.4.1.

1.4.1 Creating Objects and Calling Methods

1 boyDog = Dog("Mesa", 5, 15, 2004, "WOOOF")

2 girlDog = Dog("Sequoia", 5, 6, 2004, "barkbark")

3 print(boyDog.speak())

4 print(girlDog.speak())

5 print(boyDog.birthDate())

6 print(girlDog.birthDate())

7 boyDog.changeBark("woofywoofy")

8 print(boyDog.speak())

Once created in the memory of the computer, dog objects looks like those appearing in Fig. 1.3. Each object is referenced by the variable reference assigned to it, either girlDog or boyDog in this case. The objects themselves are a collection of references that point to the information that is stored in the object. Each object has name, month, day, year, and speakText references that point to the associated data that make up a Dog object.

To be able to create Dog objects like these two objects we need a Dog class to define these objects. In addition, we'll need to define speak, birthDate, and changeBark methods. We can do this by writing a class as shown in Sect. 1.4.2. Comments about each part of the class appear in the code. The special variable self always points at the current object and must be the first parameter to each method in the class.

Fig. 1.3 A Couple of Dog Objects

Python takes care of passing the self argument to the methods. The other arguments are passed by the programmer when the method is called (see the example of calling each method in Sect. 1.4.1).

1.4.2 The Dog Class

1 class Dog:

2 # This is the constructor for the class. It is called whenever a Dog

3 # object is created. The reference called "self" is created by Python

4 # and made to point to the space for the newly created object. Python

5 # does this automatically for us but we have to have "self" as the first

6 # parameter to the init method (i.e. the constructor).

7 def init (self, name, month, day, year, speakText):

8 = name

9 self.month = month

10 = day

11 self.year = year

12 self.speakText = speakText


14 # This is an accessor method that returns the speakText stored in the

15 # object. Notice that "self" is a parameter. Every method has "self" as its

16 # first parameter. The "self" parameter is a reference to the current

17 # object. The current object appears on the left hand side of the dot (i.e.

18 # the .) when the method is called.

19 def speak(self):

20 return self.speakText


22 # Here is an accessor method to get the name

23 def getName(self):

24 return


26 # This is another accessor method that uses the birthday information to

27 # return a string representing the date.

28 def birthDate(self):

29 return str(self.month) + "/" + str( + "/" + str(self.year)


31 # This is a mutator method that changes the speakText of the Dog object.

32 def changeBark(self,bark):

33 self.speakText = bark

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science