diff options
| author | 2023-01-25 08:45:24 -0700 | |
|---|---|---|
| committer | 2023-01-25 08:45:24 -0700 | |
| commit | a0bfba540a0fedabe3f3e3625ac829c614fd4b82 (patch) | |
| tree | 08d07a1b4b2ad984bbe6305651139e1dd1062c20 | |
| parent | Make new tag for #2360 (diff) | |
Update in-place.md
| -rw-r--r-- | bot/resources/tags/in-place.md | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/bot/resources/tags/in-place.md b/bot/resources/tags/in-place.md index 361dfd111..c81fa660a 100644 --- a/bot/resources/tags/in-place.md +++ b/bot/resources/tags/in-place.md @@ -1,5 +1,24 @@ **Out of Place** and **In Place** -- An "out of place" operation is one that creates a new object, leaving the original object unchanged. -- An "in place" operation is one that modifies the original object, without creating a new one. +- An "out of place" operation creates a new object, leaving the original object unchanged. +- An "in place" operation modifies the original object, without creating a new one. These return None explicitly. +A prime example of these different ideas is `list.sort()` vs `sorted(...)`: + +`list.sort()` can cause many errors within your code, one of the most common is shown below: + +```py +a_list = [3, 1, 2] +a_new_list = a_list.sort() # This will be None +print(a_new_list[1]) # This will error +``` + +On the other hand, `sorted()` can also cause errors: + +```py +a_list = [3, 1, 2] +sorted(a_list) +print(a_list[0]) # You may expect 1, but it will print 3 +``` + +Both of these errors are an easy fixes. |