aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar decorator-factory <[email protected]>2020-04-01 00:28:43 +0300
committerGravatar GitHub <[email protected]>2020-04-01 00:28:43 +0300
commit2559bc90517bfbd76f1a57a7a6c0dc75660c1985 (patch)
treea4d10bd68396a5cb64d3351acf03d520483c2fd8
parentMerge pull request #852 from ks129/infraction-edit (diff)
Add mutability.md tag
-rw-r--r--bot/resources/tags/mutability.md34
1 files changed, 34 insertions, 0 deletions
diff --git a/bot/resources/tags/mutability.md b/bot/resources/tags/mutability.md
new file mode 100644
index 000000000..08e28b370
--- /dev/null
+++ b/bot/resources/tags/mutability.md
@@ -0,0 +1,34 @@
+# Mutable vs immutable objects
+
+Imagine that you want to make all letters in a string upper case.
+Conveniently, strings have an `.upper()` method.
+
+You might think that this would work:
+```python
+string = "abcd"
+string.upper()
+print(string) # abcd
+```
+
+`string` didn't change. Why is that so?
+
+That's because strings in Python are _immutable_. You can't change them, you can only pass
+around existing strings or create new ones.
+
+```python
+string = "abcd"
+string = string.upper()
+```
+`string.upper()` creates a new string which is like the old one, but with all
+the letters turned to upper case.
+
+`int`, `float`, `complex`, `tuple`, `frozenset` are other examples of immutable data types in Python.
+
+Mutable data types like `list`, on the other hand, can be changed in-place:
+```python
+my_list = [1, 2, 3]
+my_list.append(4)
+print(my_list) # [1, 2, 3, 4]
+```
+
+`dict` and `set` are other examples of mutable data types in Python.