Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created September 19, 2014 15:13
Show Gist options
  • Save thomasballinger/cdd53302ee24fc3120f1 to your computer and use it in GitHub Desktop.
Save thomasballinger/cdd53302ee24fc3120f1 to your computer and use it in GitHub Desktop.
### current bpython session - file will be reevaluated, ### lines will not be run
class Mapping(object):
def __init__(self):
self.data = []
def add_key_value_pair(self, key, value):
self.data.append([key, value])
def get_value(self, key):
for k, value in self.data:
if k == key:
return value
raise KeyError
def remove_key(self, key):
for i, (k, v) in enumerate(self.data):
if k == key:
break
else:
raise KeyError
self.data.pop(i)
m = Mapping()
m.add_key_value_pair('hello', 10)
m.get_value('hello')
### 10
m.remove_key('hello')
m.remove_key('hello')
### Traceback (most recent call last):
### File "<input>", line 1, in <module>
### File "<input>", line 16, in remove_key
### KeyError
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment