Force keyword argument input with keyword name for a function

In python, we can force keyword argument input with keyword name for a function using “*” like:

In [3]: def f(a, b, c=0, d=1):
   ...:     pass
   ...:

In [4]: f(1, 2, 3, 4)

In [5]: f(1, 2, c=3, d=4)

In [6]: def f(a, b, *, c=0, d=1):
   ...:     pass
   ...:

In [7]: f(1, 2, c=3, d=4)

In [8]: f(1, 2, 3, 4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-0851ce1b6418> in <module>
----> 1 f(1, 2, 3, 4)

TypeError: f() takes 2 positional arguments but 4 were given

Is it possible to do it in Julia?

Yes, but it’s not quite the same. You can write

foo(a, b; c=0, d=1) = nothing

Then c and d are keyword arguments. But, unlike Python, you must always specify the keyword name in Julia, and you can never do it for positional arguments.

2 Likes