"Chris" wrote: > 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.
calling hash(obj) is the only sane way to figure out if calling hash(obj) will work. > 2) Would it be possible to have a "ismutable" function or method? objects are mutable only if they have some method (visible or __hidden__) that can be used to modify their contents. from the Python runtime perspec- tive, objects are objects. > 3) In this example, is t considered mutable or not? > "Tuple are immutable" says the doc, but: >>>> t[0].append(0) >>>> t > ([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! as map(id, t) can tell you, the tuple object itself wasn't modified. > 4) Even more confusing: I had the following strange result: > (with both Python 2.3.3 and 2.4) >>>> t[0]+=[1] > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: object doesn't support item assignment >>>> t > ([1, 0, 1], [2]) > There was an exception, but the list was still changed!? that's a silly side-effect of how "+=" is defined for lists; the "+" part of the expression works, and modifies the target object, but the "=" part fails. </F> -- http://mail.python.org/mailman/listinfo/python-list