1# to test keyword-only arguments 2 3# simplest case 4def f(*, a): 5 print(a) 6 7f(a=1) 8 9# with 2 keyword-only args 10def f(*, a, b): 11 print(a, b) 12 13f(a=1, b=2) 14f(b=1, a=2) 15 16# positional followed by bare star 17def f(a, *, b, c): 18 print(a, b, c) 19 20f(1, b=3, c=4) 21f(1, c=3, b=4) 22f(1, **{'b':'3', 'c':4}) 23 24try: 25 f(1) 26except TypeError: 27 print("TypeError") 28 29try: 30 f(1, b=2) 31except TypeError: 32 print("TypeError") 33 34try: 35 f(1, c=2) 36except TypeError: 37 print("TypeError") 38 39# with **kw 40def f(a, *, b, **kw): 41 print(a, b, kw) 42 43f(1, b=2) 44f(1, b=2, c=3) 45 46# with named star 47def f(*a, b, c): 48 print(a, b, c) 49 50f(b=1, c=2) 51f(c=1, b=2) 52 53# with positional and named star 54def f(a, *b, c): 55 print(a, b, c) 56 57f(1, c=2) 58f(1, 2, c=3) 59f(a=1, c=3) 60 61# lambda as kw-only arg (exposes nested behaviour in compiler) 62def f(*, x=lambda:1): 63 return x() 64print(f()) 65print(f(x=f)) 66print(f(x=lambda:2)) 67