Last active
February 9, 2021 14:33
-
-
Save sccolbert/6849a1d60277f56fecd6a59a52c73c9a to your computer and use it in GitHub Desktop.
Typed Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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