Why am I getting the previous value into the different instance variables?
For example:
d1 = myDict()
d2 = myDict()
d1.assign(2,3)
d2.assign(2,2)
print(d1.getval(2))
print(d2.getval(2))
class myDict(object):
""" Implements a dictionary without using a dictionary """
aDict = {}
def __init__(self):
""" initialization of your representation """
#FILL THIS IN
def assign(self, k, v):
""" k (the key) and v (the value), immutable objects """
#FILL THIS IN
self.k = k
self.v = v
if self.k not in myDict.aDict:
self.aDict[self.k] = self.v
else:
self.aDict[self.k] = self.v
def getval(self, k):
""" k, immutable object """
#FILL THIS IN
if k in myDict.aDict:
return myDict.aDict[k]
else:
KeyError ('KeyError successfully raised')
# return myDict.aDict[k]
def delete(self, k):
""" k, immutable object """
#FILL THIS IN
if k in myDict.aDict.keys():
del myDict.aDict[k]
else:
raise KeyError('KeyError successfully raised')
def __str__(self):
return str(myDict.aDict)
d1 = myDict()
d2 = myDict()
d1.assign(2,3)
d2.assign(2,2)
print(d1.getval(2))
print(d2.getval(2))
My output:
2
2
4
1
Correct output:
3
2
4
1