Hi
Consider the following tuples:[color=blue][color=green][color=darkred]
>>> t = ([1],[2])
>>> u = (1,2)
>>> v = ('1','2')
>>> t[/color][/color][/color]
([1], [2])[color=blue][color=green][color=darkred]
>>> u[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> v[/color][/color][/color]
('1', '2')[color=blue][color=green][color=darkred]
>>> t.__hash__()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable[color=blue][color=green][color=darkred]
>>> u.__hash__()[/color][/color][/color]
219750523[color=blue][color=green][color=darkred]
>>> v.__hash__()[/color][/color][/color]
-1786881095[color=blue][color=green][color=darkred]
>>> d = dict()
>>> d[t] = 't'[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable[color=blue][color=green][color=darkred]
>>> d[u] = 'u'
>>> d[v] = 'v'
>>> d[/color][/color][/color]
{('1', '2'): 'v', (1, 2): 'u'}
t, u and v are all tuples. t and v elements are sequences.
Yet, t cannot be a dictionnary key because its elements are mutable.
1) Given a tuple, how can I know if it can be a dictionnary key or not?
Of course I could call __hash__ and catch for a TypeError exception,
but I'm looking for a better way to do it.
2) Would it be possible to have a "ismutable" function or method? Like:[color=blue][color=green][color=darkred]
>>> t.ismutable()[/color][/color][/color]
True, well maybe not...[color=blue][color=green][color=darkred]
>>> u.ismutable()[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> u.ismutable()[/color][/color][/color]
False
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:[color=blue][color=green][color=darkred]
>>> t[0].append(0)
>>> t[/color][/color][/color]
([1, 0], [2])
The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!
4) Even more confusing: I had the following strange result:
(with both Python 2.3.3 and 2.4)[color=blue][color=green][color=darkred]
>>> t[0]+=[1][/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment[color=blue][color=green][color=darkred]
>>> t[/color][/color][/color]
([1, 0, 1], [2])
There was an exception, but the list was still changed!?
Chris
Consider the following tuples:[color=blue][color=green][color=darkred]
>>> t = ([1],[2])
>>> u = (1,2)
>>> v = ('1','2')
>>> t[/color][/color][/color]
([1], [2])[color=blue][color=green][color=darkred]
>>> u[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> v[/color][/color][/color]
('1', '2')[color=blue][color=green][color=darkred]
>>> t.__hash__()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable[color=blue][color=green][color=darkred]
>>> u.__hash__()[/color][/color][/color]
219750523[color=blue][color=green][color=darkred]
>>> v.__hash__()[/color][/color][/color]
-1786881095[color=blue][color=green][color=darkred]
>>> d = dict()
>>> d[t] = 't'[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable[color=blue][color=green][color=darkred]
>>> d[u] = 'u'
>>> d[v] = 'v'
>>> d[/color][/color][/color]
{('1', '2'): 'v', (1, 2): 'u'}
t, u and v are all tuples. t and v elements are sequences.
Yet, t cannot be a dictionnary key because its elements are mutable.
1) Given a tuple, how can I know if it can be a dictionnary key or not?
Of course I could call __hash__ and catch for a TypeError exception,
but I'm looking for a better way to do it.
2) Would it be possible to have a "ismutable" function or method? Like:[color=blue][color=green][color=darkred]
>>> t.ismutable()[/color][/color][/color]
True, well maybe not...[color=blue][color=green][color=darkred]
>>> u.ismutable()[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> u.ismutable()[/color][/color][/color]
False
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:[color=blue][color=green][color=darkred]
>>> t[0].append(0)
>>> t[/color][/color][/color]
([1, 0], [2])
The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!
4) Even more confusing: I had the following strange result:
(with both Python 2.3.3 and 2.4)[color=blue][color=green][color=darkred]
>>> t[0]+=[1][/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment[color=blue][color=green][color=darkred]
>>> t[/color][/color][/color]
([1, 0, 1], [2])
There was an exception, but the list was still changed!?
Chris
Comment