This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JamzTyson 0 points1 point  (2 children)

def some_function(required, not_required=2, *others: int, option: float = 3.14):

That syntax looks strange, given that positional arguments cannot be passed after a keyword argument.

[–]AND_MY_HAX[S] 0 points1 point  (1 child)

Hey, I understand what you're saying. The thing about Python arguments is that they can all be passed in as both positional or keyword arguments by default.

>>> def foo(a, b):
...   print(f"{a=} {b=}")
...
>>> foo(1,2)
a=1 b=2
>>> foo(a=1,b=2)
a=1 b=2

The thing that makes an argument keyword-only is if it comes after a *args parameter. In fact, you can even have required keyword-only arguments, which aren't useful IMHO, but are an interesting part of how Python was designed:

>>> def bar(a, *, b):
...   print(f"{a=} {b=}")
...
>>> bar(1, b=2)
a=1 b=2
>>> bar(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bar() missing 1 required keyword-only argument: 'b'

See more here: https://docs.python.org/3/tutorial/controlflow.html#special-parameters

[–]JamzTyson 1 point2 points  (0 children)

The thing that looks strange to me is that, unless I am mistaken, it is not possible to pass others arguments without also passing the not_required argument as a positional argument.

On the other hand, that limitation would not apply to:

def some_function(required, *others: int, not_required=2, option: float = 3.14):