Lichen

tests/dict.py

360:209dc7a270fd
2016-12-09 Paul Boddie Added support for dynamic attribute access using getattr and hasattr, employing a special attribute on strings to hold the object table code and position for any attribute having the same name as the represented string.
     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}     9 print d    10 print d[10]                             # a    11 print d[20]                             # b    12 print d["c"]                            # 30    13 try:    14     print d[30]                         # should fail with an exception    15 except KeyError, exc:    16     print "d[30]: key not found", exc.key    17     18 l = f(d)    19 print l    20 print 10 in l                          	# True    21 print 20 in l                          	# True    22 print "c" in l                          # True    23 print 30 in l                          	# False    24     25 l = d.values()    26 print l    27 print "a" in l                          # True    28 print "b" in l                          # True    29 print 30 in l                           # True    30 print "c" in l                          # False    31     32 v = g(d) # either "a" or "b" or 30    33 print v    34 print v == "a" or v == "b" or v == 30   # True    35 print v == 10 or v == 20 or v == "c"    # False    36     37 l = d.items()    38 print l    39 print (10, "a") in l                    # True    40 print ("c", 30) in l                    # True    41 print (10, "b") in l                    # False