December 2018
Beginner to intermediate
796 pages
19h 54m
English
Python 3 allows for a new type of parameter: the keyword-only parameter. We are going to study them only briefly as their use cases are not that frequent. There are two ways of specifying them, either after the variable positional arguments, or after a bare *. Let's see an example of both:
# arguments.keyword.only.pydef kwo(*a, c): print(a, c)kwo(1, 2, 3, c=7) # prints: (1, 2, 3) 7kwo(c=4) # prints: () 4# kwo(1, 2) # breaks, invalid syntax, with the following error# TypeError: kwo() missing 1 required keyword-only argument: 'c'def kwo2(a, b=42, *, c): print(a, b, c)kwo2(3, b=7, c=99) # prints: 3 7 99kwo2(3, c=13) # prints: 3 42 13# kwo2(3, 23) # breaks, invalid syntax, with the following error# TypeError: kwo2() missing ...Read now
Unlock full access