aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Anand Krishna <[email protected]>2021-02-05 19:39:36 +0400
committerGravatar GitHub <[email protected]>2021-02-05 19:39:36 +0400
commit2c4d7f41432bb620d83d3403fe4ab9317bc1129f (patch)
tree5b629341f358a606b238e259d3fc573642e3dc8e
parentMake `KeyError` tag (diff)
Update and rename keyerror.md to dict-get.md
-rw-r--r--bot/resources/tags/dict-get.md13
-rw-r--r--bot/resources/tags/keyerror.md17
2 files changed, 13 insertions, 17 deletions
diff --git a/bot/resources/tags/dict-get.md b/bot/resources/tags/dict-get.md
new file mode 100644
index 000000000..b22db7af5
--- /dev/null
+++ b/bot/resources/tags/dict-get.md
@@ -0,0 +1,13 @@
+Often while using dictionaries in Python, you may run into `KeyErrors`. This error is raised when you try to access a key that isn't present in your dictionary.\
+While you can use a `try` and `except` block to catch the `KeyError`, Python also gives you some other neat ways to handle them.
+__**The `dict.get` method**__
+The [`dict.get`](https://docs.python.org/3/library/stdtypes.html#dict.get) method will return the value for the key if it exists, or None (or a default value that you specify) if the key doesn't exist. Hence it will _never raise_ a KeyError.
+```py
+>>> my_dict = {"foo": 1, "bar": 2}
+>>> print(my_dict.get("foobar"))
+None
+>>> print(my_dict.get("foobar", 3)) # here 3 is the default value to be returned, in case the key doesn't exist
+3
+```
+
+Some other methods that can be used for handling KeyErrors gracefully are the [`dict.setdefault`](https://docs.python.org/3/library/stdtypes.html#dict.setdefault) method, or by using [`collections.defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict).
diff --git a/bot/resources/tags/keyerror.md b/bot/resources/tags/keyerror.md
deleted file mode 100644
index d0c069004..000000000
--- a/bot/resources/tags/keyerror.md
+++ /dev/null
@@ -1,17 +0,0 @@
-Often while using dictionaries in Python, you may run into `KeyErrors`. This error is raised when you try to access a key that isn't present in your dictionary. \
-While you can use a `try` and `except` block to catch the `KeyError`, Python also gives you some other neat ways to handle them.
-## __The `dict.get` method__
-The [dict.get](https://docs.python.org/3/library/stdtypes.html#dict.get) method will return the value for the key if it exists, or None (or a default value that you specify) if the key doesn't exist. Hence it will _never raise_ a KeyError.
-```py
->>> my_dict = {"foo": 1, "bar": 2}
->>> print(my_dict.get("foo"))
-1
->>> print(my_dict.get("foobar"))
-None
->>> print(my_dict.get("foobar", 3)) # here 3 is the default value to be returned, in case the key doesn't exist
-3
->>> print(my_dict)
-{'foo': 1, 'bar': 2} # note that the new key was NOT added to the dictionary
-```
-\
-Some other methods that can be used for handling KeyErrors gracefully are the [dict.setdefault](https://docs.python.org/3/library/stdtypes.html#dict.setdefault) method, or by using [collections.defaultdict](https://docs.python.org/3/library/collections.html#collections.defaultdict).