aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Boris Muratov <[email protected]>2023-03-30 11:00:02 +0300
committerGravatar GitHub <[email protected]>2023-03-30 11:00:02 +0300
commit1c993559b53f0d4949bc3c4c49dd36a7bbf9779d (patch)
treed3cbdb24e78302f6eb4b46c54c9737619a7d06fe
parentBump regex from 2022.10.31 to 2023.3.23 (#2485) (diff)
parentMerge branch 'main' into inplace-tag (diff)
Merge pull request #2388 from brodycritchlow/inplace-tag
Add new inplace tag
-rw-r--r--bot/resources/tags/in-place.md22
1 files changed, 22 insertions, 0 deletions
diff --git a/bot/resources/tags/in-place.md b/bot/resources/tags/in-place.md
new file mode 100644
index 000000000..e2218a7df
--- /dev/null
+++ b/bot/resources/tags/in-place.md
@@ -0,0 +1,22 @@
+---
+embed:
+ title: "Out of place and in place"
+---
+
+In programming, there are two types of operations: "out of place" operations create a new object, leaving the original object unchanged. "in place" operations modify the original object without creating a new one, and return `None` explicitly.
+
+A common example of these different concepts is seen in the use of the methods `list.sort()` and `sorted(...)`. Using `list.sort()` and attempting to access an element of the list will result in an error.
+
+```py
+# WRONG:
+
+unsorted_list = [3, 1, 2]
+sorted_list = unsorted_list.sort() # This will be None
+print(sorted_list) # Outputs None. Where did the list go?
+
+list_to_sort = [3, 1, 2]
+sorted(list_to_sort)
+print(list_to_sort) # The list still isn't sorted. Why?
+```
+
+To avoid these errors and unexpected results, you should either use an out-of-place operation `(sorted(...))` and assign it to a variable or use an in-place operation `(list.sort())` without assignment.