Skip to content

Instantly share code, notes, and snippets.

@sccolbert
Last active February 9, 2021 14:33
Show Gist options
  • Save sccolbert/6849a1d60277f56fecd6a59a52c73c9a to your computer and use it in GitHub Desktop.
Save sccolbert/6849a1d60277f56fecd6a59a52c73c9a to your computer and use it in GitHub Desktop.
Typed Map
class TypedMap:
""" A class which implements a typed map.
"""
def __init__(key_type, value_type):
pass
# Implement the methods required
# for the functionality demonstrated
# in the __main__ clause
if __name__ == '__main__':
map = TypedMap(int, string)
print(map[1]) # raises a KeyError
map[1] = '42'
print(map[1]) # prints 42
map[1] = 42 # raises a TypeError
map[2] = 'two'
map[3] = 'three'
map['cat'] = 'dog' # raises a TypeError
print(map) # prints a string representation of the map
for key in map: # prints each key in the map
print(key)
print(len(map)) # prints the length of the map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment