Setting up the Key-Value Store Class
For our simple key-value store, we'll start by creating a Python class called KeyValueStore
. This class will serve as the foundation for our store and will initially only contain an empty dictionary to hold our keys and values.
PYTHON
1class KeyValueStore:
2 def __init__(self):
3 self.store = {}
In the above code, the KeyValueStore
class has a constructor (__init__
method) that initializes an empty dictionary, store
. This dictionary will play the role of our in-memory key-value store, much like Python's dict or a HashMap in Java.
Next, let's instantiate our KeyValueStore
and confirm that it's been created properly:
PYTHON
1if __name__ == '__main__':
2 kvStore = KeyValueStore()
3 print('KeyValueStore instance created:', kvStore)
This code creates an instance of our KeyValueStore
class and prints a confirmation message. At this point, the store
attribute is an empty dictionary.
xxxxxxxxxx
class KeyValueStore:
def __init__(self):
self.store = {}
if __name__ == '__main__':
kvStore = KeyValueStore()
print('KeyValueStore instance created:', kvStore)
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment