teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

コード追記

2020/05/17 06:22

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -26,4 +26,6 @@
26
26
  >>> log.update({'username': 'test_user'})
27
27
  >>> print(log)
28
28
  {'id': 'title', 'username': 'test_user'}
29
+ >>> print(threads[0])
30
+ {'id': 'title'}
29
31
  ```

4

辞書破棄コード追記

2020/05/17 06:22

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -7,4 +7,23 @@
7
7
  logging.debug(log)
8
8
  ```
9
9
 
10
- と変更してみたら結果はどうなりますか?
10
+ と変更してみたら結果はどうなりますか?
11
+
12
+ updateした辞書データが破棄される状況は、これで伝わるでしょうか?
13
+
14
+ ```python
15
+ >>> class Sample:
16
+ ... def __getitem__(self, index):
17
+ ... return {'id': 'title'}
18
+ ...
19
+ >>> threads = Sample()
20
+ >>> print(threads[0])
21
+ {'id': 'title'}
22
+ >>> threads[0].update({'username': 'test_user'})
23
+ >>> print(threads[0])
24
+ {'id': 'title'}
25
+ >>> log = threads[0]
26
+ >>> log.update({'username': 'test_user'})
27
+ >>> print(log)
28
+ {'id': 'title', 'username': 'test_user'}
29
+ ```

3

コード変更

2020/05/17 06:16

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -2,7 +2,7 @@
2
2
  メソッドの中で毎回辞書データを生成して返しているのであれば、updateした辞書はすぐに破棄されてしまいます。
3
3
 
4
4
  ```python
5
- log = threads[0]
5
+ log = dict(threads[0])
6
6
  log.update(username_dicts[0])
7
7
  logging.debug(log)
8
8
  ```

2

コード修正

2020/05/17 04:29

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -2,7 +2,7 @@
2
2
  メソッドの中で毎回辞書データを生成して返しているのであれば、updateした辞書はすぐに破棄されてしまいます。
3
3
 
4
4
  ```python
5
- log = dict(threads[0])
5
+ log = threads[0]
6
6
  log.update(username_dicts[0])
7
7
  logging.debug(log)
8
8
  ```

1

説明変更

2020/05/17 04:28

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  `threads[0]` は、`threads.__getitem__(0)` というメソッド呼び出しをしています。
2
- メソッドの中で毎回インスンスを生成して返しているのであれば、updateした辞書はすぐに破棄されてしまいます。
2
+ メソッドの中で毎回辞書データを生成して返しているのであれば、updateした辞書はすぐに破棄されてしまいます。
3
3
 
4
4
  ```python
5
5
  log = dict(threads[0])