Programming Essentials Mutator Method Explain the purpose of the mutator method.
ID: 667650 • Letter: P
Question
Programming Essentials
Mutator Method
Explain the purpose of the mutator method. What does it normally return and why?
Explain aliasing, how does it affect the mutation of an object?
Give an example of an object that behaves like a dictionary show a code example of how the python dictionary reflects the real world example.
Programming Essentials
Mutator Method
Explain the purpose of the mutator method. What does it normally return and why?
Explain aliasing, how does it affect the mutation of an object?
Give an example of an object that behaves like a dictionary show a code example of how the python dictionary reflects the real world example.
Explanation / Answer
Mutator Method
Just like any other object oriented programming language, Objects are used to store information. It has state and behavior. There are two kinds of methods in object oriented programming language: accessor and mutator methods.
These are like setter and getter methods.
Accessor method is used to access the current state of the object but don’t change it.
For example:
a = “Hello world”
b = a.upper()
print(b)
Now, here you can notice that upper method is used to change “a” to upper case and store it into “b” but it’s not changing object “a”.
Now, Mutator method is used to change the existing object.
For example:
sampleList= {10, 20, 30 }
sampleList.reverse()
print(sampleList)
This will print list in reverse manner, i.e. {30, 20, 10} to the screen. The change done by mutator method is permanent.
Aliasing in Python
Alias means another name of an entity. It is easy to make alias of data.
If data is immutable, alias doesn’t matter.
But if data is mutable then it can lead to unrecognizable bugs.
So, using mutator method on aliased objects can lead to bugs.
For example:
Lists are mutable in python.
x = [‘apple’]
y = x
x = x.append(‘orange’)
print first
[‘apple’, ‘orange’]
print second
[‘apple’, ‘orange’]
As both objects are having same reference to the value. Changing one object can explicitly modify second object as a side effect.
Python Dictionary
In python, it is called as “dicts”. In other languages, it is referred as “hashes” or “maps”.
It is use to data in key-value pair.
Let understand by comparing dictionary with list.
For example:
In case of list, we use index to store or identify its element.
>>> values = ['a', 'b', 'c', 'd']
>>> print values[1]
b
>>> values[1] = 'y'
>>> print values[1]
z
>>> values
['a', 'y', 'c', 'd']
In case of dictionary, we can use anything to associate one thing to another.
>>> values = {'name': 'Smith', 'age': 21, 'height': 5 * 12 + 6}
>>> print values['name']
Smith
>>> print values['age']
21
>>> print values['height']
66
>>> values['city'] = "New york"
>>> print values['city']
New york
To add values in dictionary, see below example
>>> values[1] = "Apple"
>>> values[2] = "Orange"
>>> print values[1]
Apple
>>> print values[2]
Orange
>>> values
{'city': 'New york', 2: 'Orange', 'name': 'Smith', 1: 'Orange', 'age': 21, 'height': 66}
To delete values from dictionary, see below example
>>> del values['city']
>>> del values[1]
>>> del values[2]
>>> values
{'name': 'Smith', 'age': 21, 'height': 66}
Example of Dictionary in python
Save it as sample.py and run as $ python sample.py
_______________________________________________________________________________________________
# create a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}
# create a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
# print out some cities
print '-' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']
# print some states
print '-' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: ", states['Florida']
# do it by using the state then cities dict
print '-' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]
# print every state abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)
# print every city in state
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)
# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])
print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas')
if not state:
print "Sorry, no Texas."
# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city
_______________________________________________________________________________________________
Real life example of Dictionary can be English word Dictionary like Oxford Dictionary,
Internet Address Lookup Table
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.