fun_kwonlydef.py 696 B

123456789101112131415161718192021222324252627282930313233343536
  1. # test function args, keyword only with default value
  2. # a single arg with a default
  3. def f1(*, a=1):
  4. print(a)
  5. f1()
  6. f1(a=2)
  7. # 1 arg default, 1 not
  8. def f2(*, a=1, b):
  9. print(a, b)
  10. f2(b=2)
  11. f2(a=2, b=3)
  12. # 1 positional, 1 arg default, 1 not
  13. def f3(a, *, b=2, c):
  14. print(a, b, c)
  15. f3(1, c=3)
  16. f3(1, b=3, c=4)
  17. f3(1, **{'c':3})
  18. f3(1, **{'b':'3', 'c':4})
  19. # many args, not all with defaults
  20. def f4(*, a=1, b, c=3, d, e=5, f):
  21. print(a, b, c, d, e, f)
  22. f4(b=2, d=4, f=6)
  23. f4(a=11, b=2, d=4, f=6)
  24. f4(a=11, b=2, c=33, d=4, e=55, f=6)
  25. f4(f=6, e=55, d=4, c=33, b=2, a=11)
  26. # positional with default, then keyword only
  27. def f5(a, b=4, *c, d=8):
  28. print(a, b, c, d)
  29. f5(1)
  30. f5(1, d=9)
  31. f5(1, b=44, d=9)