Lichen

tests/get_using.py

934:2989aab1b4f7
6 months ago Paul Boddie Renamed the utf8string class to unicode, eliminating the unicode function. This means that the simple case of merely returning an object if it is already a Unicode object no longer occurs when using the unicode callable, but such behaviour might be better supported with more general customised instantiation functionality.
     1 class C:     2     def __init__(self):     3         self.x = 123     4      5     def f(self):     6         return self.x     7      8 class D:     9     pass    10     11 c = C()    12 f = C.f    13 fn = get_using(C.f, c)    14 print fn                            # __main__.C.f    15 print fn()                          # 123    16     17 fn = get_using(C.f, C)    18 print fn                            # __main__.C.f    19 try:    20     print fn()                      # fails    21 except UnboundMethodInvocation:    22     print "fn(): method is unbound"    23     24 try:    25     print f()                       # fails    26 except UnboundMethodInvocation:    27     print "f(): method is unbound"    28     29 d = D()    30 try:    31     fn = get_using(C.f, d)    32 except TypeError:    33     print "get_using(C.f, d): d is not compatible with C"    34     35 fn = get_using(c, C.f)    36 print fn                            # <__main__.C instance>    37 try:    38     print fn()                      # fails    39 except TypeError:    40     print "fn(): object is not callable"