回答編集履歴
1
説明追加
answer
CHANGED
@@ -22,4 +22,28 @@
|
|
22
22
|
|
23
23
|
([])は[]と同一なので、ミュータブルです。
|
24
24
|
|
25
|
-
空リストを要素とするタプルは、([],)と書きます。
|
25
|
+
空リストを要素とするタプルは、([],)と書きます。
|
26
|
+
|
27
|
+
([],)はタプルなのでイミュータブルですが、その要素の[]はミュータブルです。
|
28
|
+
```python
|
29
|
+
>>> x =([],)
|
30
|
+
>>> print(x)
|
31
|
+
([],)
|
32
|
+
>>> x[0].append(42)
|
33
|
+
>>> print(x)
|
34
|
+
([42],)
|
35
|
+
```
|
36
|
+
このようなオブジェクトをunhashableあるいはhashableでないといいます。
|
37
|
+
|
38
|
+
unhashableなオブジェクトはdictのキーやsetの要素になることができません。
|
39
|
+
|
40
|
+
```python
|
41
|
+
>>> d = {x:1}
|
42
|
+
Traceback (most recent call last):
|
43
|
+
File "<stdin>", line 1, in <module>
|
44
|
+
TypeError: unhashable type: 'list'
|
45
|
+
>>> s = set(x)
|
46
|
+
Traceback (most recent call last):
|
47
|
+
File "<stdin>", line 1, in <module>
|
48
|
+
TypeError: unhashable type: 'list'
|
49
|
+
```
|