C
Chris
Hi
Consider the following tuples:
{('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:False
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
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)
([1, 0, 1], [2])
There was an exception, but the list was still changed!?
Chris
Consider the following tuples:
Traceback (most recent call last):t = ([1],[2])
u = (1,2)
v = ('1','2')
t ([1], [2])
u (1, 2)
v ('1', '2')
t.__hash__()
Traceback (most recent call last):File said:u.__hash__() 219750523
v.__hash__() -1786881095
d = dict()
d[t] = 't'
File said:d = 'u'
d[v] = 'v'
d
{('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:False
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
([1, 0], [2])t[0].append(0)
t
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)
Traceback (most recent call last):
([1, 0, 1], [2])
There was an exception, but the list was still changed!?
Chris