News for hackers. Stuff that matters.
Re:Don't Use Booleans by fernandotakai on Tuesday July 09, 2024 @04:57AM (#40913897)
you can force named arguments in python by using * after any non-named args. example:

    In [1]: def my_func(first, *, second, third):
       ...:     print(first, second, third)
       ...:

    In [2]: my_func(1, second=2, third=3)
    1 2 3

    In [3]: my_func(1, 2, 3)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 my_func(1, 2, 3)

    TypeError: my_func() takes 1 positional argument but 3 were given

    In [4]: def my_func_2(*, first, second, third):
       ...:     print(first, second, third)
       ...:

    In [5]: my_func_2(first=1, second=2, third=3)
    1 2 3

    In [6]: my_func_2(1, second=2, third=3)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    Cell In[6], line 1
    ----> 1 my_func_2(1, second=2, third=3)

    TypeError: my_func_2() takes 0 positional arguments but 1 positional argument (and 2 keyword-only arguments) were given

There are no comments on this comment yet.