Skip to content

Commit 5aad8b7

Browse files
committed
adding session 7 into the index!
1 parent 1a0e6de commit 5aad8b7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

materials/source/index.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ Week 6: November 7
6464
Week 7: November 14
6565
...................
6666

67-
November 21: Thanksgiving week -- no class
67+
:ref:`session_1_07`
68+
69+
:ref:`notes_session07`
70+
71+
**November 21: Thanksgiving week -- no class**
6872

6973
Week 8: November 28
7074
...................

materials/source/notes/session07.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,41 @@ Zandra Eng
2222
Issues that came up during the week.
2323
====================================
2424

25+
Mutating vs. re-assigning
26+
-------------------------
27+
28+
I've seen code like this in a few trigram solutions::
29+
30+
output = output + [follower]
31+
32+
(``output`` is a list of strings, follower is a single string)
33+
34+
What it does is add a new item to a list.
35+
36+
But is that an efficient way to do that?
37+
38+
If you are adding one element to a list -- append() is the way to go. This works fine, but it's creating a whole new list just to throw it away again:
39+
40+
output_list.append (random_trigram_followers)
41+
42+
and if you are adding another list of objects, you want to use extend(). The way it is now, you are actually doing:
43+
44+
1) create a list with random_trigram_followers in it.
45+
2) create a new list with the contents of output_list the new list.
46+
3) re-assign the name output_list to that new list.
47+
4) throw away the original output_list and the temporary list you created for random_trigram_followers
48+
49+
That's a LOT of overhead!
50+
51+
Be cognizant of when you are mutating (changing) an object vs creating a new one and assigning it to the same name. When you do assignment (=) you are probably creating a new object.
52+
53+
54+
+= is different -- it is the "in_place" operator, so:
55+
56+
a_list += another_list
57+
58+
does not create an new lists -- it adds to the original list "in place" -- it is identical to:
59+
60+
a_list.extend(another_list)
61+
62+
And is an efficient operation.

0 commit comments

Comments
 (0)