File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,25 @@ Basics
5959 assert store.a_dictionary[' new_value' ] == " old value"
6060 assert store.a_dictionary is not store.a_dictionary
6161
62+ # Appending to, extending a list
63+ >> > store.list = [1 , 2 , 3 ]
64+
65+ # Because of the fact that .append won't modify the list in the actual file,
66+ # but only a copy...
67+ >> > store.list.append(4 )
68+ >> > store.list
69+ [1 , 2 , 3 ]
70+
71+ # ... we need to rather use the += operator to append to a list.
72+ >> > store.list += [4 ]
73+ >> > store.list
74+ [1 , 2 , 3 , 4 ]
75+
76+ # Similarly, we can extend the list
77+ >> > store.list += [5 , 6 ]
78+ >> > store.list
79+ [1 , 2 , 3 , 4 , 5 , 6 ]
80+
6281 Transactions
6382~~~~~~~~~~~~
6483
Original file line number Diff line number Diff line change @@ -284,6 +284,18 @@ def test_transaction_write(self):
284284 with open (self ._store_file ) as handle :
285285 self .assertEqual (handle .read (), '{"value1": 1, "value2": 2}' )
286286
287+ def test_list_concat_inplace (self ):
288+ self .store .list = []
289+ extension = [{"key" : "value" }]
290+
291+ # make sure += happens
292+ self .store ["list" ] += extension
293+ self .store .list += extension
294+ self .assertEqual (self .store .list , extension * 2 )
295+
296+ # make sure a deepcopy occurred
297+ self .assertIsNot (self .store .list [0 ], extension [0 ])
298+
287299
288300if __name__ == "__main__" :
289301 unittest .main ()
You can’t perform that action at this time.
0 commit comments