Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Properties, Accessors and Mutators

Program gif continues the definition of the Complex class. It defines four methods, getReal, setReal, getImag and setImage, as well as two properties, real and imag.

   program56995
Program: Complex class real and imag properties.

The getReal and setImag methods provide the means to access the _real and _imag instance attributes, respectively. A method that accesses an instance but does not modify that instance is called an accessor . Therefore, getReal and setReal are accessors.

The setReal and setImag methods provide the means to modify the _real and _imag instance attributes, respectively. A method that modifies an instance is called a mutator . Therefore, setReal and setImage are mutators.

The dot operator is used to specify the object on which a method is to be invoked. For example, the sequence of statements

c.setReal(2)
print c.getReal()
is equivalent to
Complex.setReal(c, 2)
print Complex.getReal(c)

Program gif also defines two properties called real and imag.

A Python property is an class attribute that provides an accessor method and/or a mutator method. The fget argument of a property specifies an accessor method called the ``getter '' and the fset argument of a property specifies a mutator method called the ``setter ''.

For example, in Program gif, the real property getter is the getReal accessor method and the real property setter is the setReal mutator method. Similarly, the imag property getter is getImag and the imag property setter is setReal.

Properties let you use instance attribute notation rather than method call notation to invoke an accessor or a mutator method. Again, the dot operator is used to specify the object on which the operation is performed. For example, the sequence of statements

c.imag = 2
print c.imag
is equivalent to
Complex.setImag(c, 2)
print Complex.getImag(c)

Program gif defines four methods and two more properties of the Complex class. The r property uses the getR accessor as its getter and the setR mutator as its setter. Similarly, the theta property uses the getTheta accessor as its getter and the setTheta mutator as its setter.

   program57059
Program: Complex class r and theta properties.

By defining suitable properties, it is possible to hide the implementation of the class from the user of that class. Consider the following statements:

print c._real
print c.real
The first statement depends on the implementation of the Complex class. If we change the implementation of the class from the one given (which uses rectangular coordinates) to one that uses polar coordinates, then the first statement above must also be changed. On the other hand, the second statement does not need to be modified, provided we reimplement the real property when we switch to polar coordinates.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.