A method defined within a class can either be an Accessor or a Mutator method.
Accessor method returns the information about the object, but do not change the state or the object.
Mutator method, also called an
Update method, can change the state of the object.
Consider the following example:
In : a = [1,2,3,4,5] In : a.count(1) Out: 1 In : a.index(2) Out: 1 In : a Out: [1, 2, 3, 4, 5] In : a.append(6) In : a Out: [1, 2, 3, 4, 5, 6]
a.index() are both
Accessor methods since it doesn’t alter the object
a in any sense, but only pulls the relevant information.
a.append() is a mutator method, since it effectively changes the object (list
a) to a new one.
In short, knowing the behavior of a method is helpful to understand how it alters the objects it acts upon.