Sep-10-2017, 01:00 AM
(Sep-10-2017, 12:23 AM)Skaperen Wrote: so how can a definition of an argument specifying that it can be given either way define the difference between a required argument and one that is not required (e.g. calling f() above with just one positional argument)?It's only forcing that last argument has to be a key word argument.
λ ptpython >>> def f(positional, either_way_with_default_value='foo',*, only_as_a_named_option='bar'): ... return positional,either_way_with_default_value,only_as_a_named_option >>> f(1) (1, 'foo', 'bar') >>> f(1, 2) (1, 2, 'bar') >>> f(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes from 1 to 2 positional arguments but 3 were given f() takes from 1 to 2 positional arguments but 3 were given >>> f(1, 2, only_as_a_named_option=3) (1, 2, 3)If remove
,*,
.>>> def f(positional, either_way_with_default_value='foo', only_as_a_named_option='bar'): ... return positional,either_way_with_default_value,only_as_a_named_option >>> f(1) (1, 'foo', 'bar') >>> f(1, 2) (1, 2, 'bar') >>> f(1, 2, 3) (1, 2, 3)