fork download
  1. def f(a, *, b):
  2. print(a, b)
  3.  
  4. f(a=1, b=2)
  5. print("f(a=1, b=2) works even though a has no default value")
  6. try:
  7. f(3, 4)
  8. except TypeError:
  9. print("f(3, 4) raises an error because b is keyword-only")
  10.  
  11. def g(x=1):
  12. print(x)
  13.  
  14. g(5)
  15. print("g(5) succeeds passing x positionally even though it has a default")
Success #stdin #stdout 0.04s 9308KB
stdin
Standard input is empty
stdout
1 2
f(a=1, b=2) works even though a has no default value
f(3, 4) raises an error because b is keyword-only
5
g(5) succeeds passing x positionally even though it has a default