Lichen

tests/dict.py

588:316c1c90b643
2017-02-17 Paul Boddie Eliminated superfluous accessor instructions loading static invocation targets. Added a test of nested calls employing explicitly retained contexts. method-wrapper-for-context
     1 def f(d):     2     return d.keys()     3      4 def g(d):     5     for key, value in d.items():     6         return value     7      8 d = {10 : "a", 20 : "b", "c" : 30, (1, 2) : "d"}     9 print "# d:",    10 print d    11 print d[10]                             # a    12 print d[20]                             # b    13 print d["c"]                            # 30    14 print d[(1, 2)]                         # d    15 try:    16     print d[30]                         # should fail with an exception    17 except KeyError, exc:    18     print "d[30]: key not found", exc.key    19 print d.get(30)                         # None    20 print d.get(30, "c")                    # c    21 print d.has_key(20)                     # True    22 print d.has_key(30)                     # False    23     24 l = f(d)    25 print "# l:",    26 print l    27 print 10 in l                          	# True    28 print 20 in l                          	# True    29 print "c" in l                          # True    30 print "d" in l                          # False    31 print 30 in l                          	# False    32 print (1, 2) in l                       # True    33     34 l = d.values()    35 print "# l:",    36 print l    37 print "a" in l                          # True    38 print "b" in l                          # True    39 print "c" in l                          # False    40 print "d" in l                          # True    41 print 30 in l                           # True    42 print (1, 2) in l                       # False    43     44 v = g(d) # either "a" or "b" or 30 or "d"    45 print "# v:",    46 print v    47 print v == "a" or v == "b" or v == 30 or v == "d"   # True    48 print v == 10 or v == 20 or v == "c" or v == (1, 2) # False    49     50 l = d.items()    51 print "# l:",    52 print l    53 print (10, "a") in l                    # True    54 print ("c", 30) in l                    # True    55 print ((1, 2), "d") in l                # True    56 print (10, "b") in l                    # False    57     58 # Try to put a list key in a dictionary.    59     60 try:    61     d[[1, 2]] = "e"    62     print d[[1, 2]]    63 except TypeError:    64     print "d[[1, 2]]: key not appropriate"    65     66 # Attempt to remove items.    67     68 del d[20]    69 print d.has_key(20)                     # False    70 try:    71     del d[30]                           # should fail with an exception    72 except KeyError, exc:    73     print "del d[30]: key not found", exc.key    74     75 # Clear the dictionary.    76     77 d.clear()    78 print d                                 # {}