changeset 54:0315da3546b3

Added a note about dictionary keys being any immutable.
author Atul Varma <varmaa@toolness.com>
date Tue, 10 Jun 2008 23:52:50 -0700
parents 89325a6a204d
children 8f019284f1d1
files PythonForJsProgrammers.txt
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/PythonForJsProgrammers.txt	Tue Jun 10 23:49:06 2008 -0700
+++ b/PythonForJsProgrammers.txt	Tue Jun 10 23:52:50 2008 -0700
@@ -488,6 +488,18 @@
     >>> "Hello %(name)s, I need %(money)d dollars." % d
     'Hello bob, I need 5 dollars.'
 
+Keys for dictionaries can actually be any immutable type; this means
+that, for instance, tuples can be used as keys:
+
+    >>> a = {(1,2) : 1}
+
+But lists can't:
+
+    >>> b = {[1,2] : 1}
+    Traceback (most recent call last):
+    ...
+    TypeError: list objects are unhashable
+
 Python dictionaries generally aren't used to create arbitrary objects
 like they are in Javascript; they don't have prototypes, nor do they
 have meta-methods.  Instead, classes are used to do that sort of