Commit 1ca940f7 authored by goekce's avatar goekce
Browse files

add positional only arguments

parent 74b3214b
Pipeline #13400 passed with stage
in 6 minutes and 45 seconds
......@@ -491,9 +491,9 @@ A) I don't know
---
In a variadic an positional argument mix, we have to specify a non-variadic argument by its name.
In a variadic and positional argument mix, we have to specify a non-variadic argument by its name.
`separator='\n'` is a keyword argument, the rest are positional arguments. Keyword arguments must be specified before keyword arguments. The reason could be that positional arguments can be identified by their position, but if we provide a keyword argument before or in the middle of positional arguments, this worsens the readability.
`separator='\n'` is a keyword argument, the rest are positional arguments. Positional arguments must be specified before keyword arguments. The reason could be that positional arguments can be identified by their position, and if we provide a keyword argument before or in the middle of positional arguments, this worsens the readability.
`concat('a', 'b', 'c', '\n')`{.py} has correct syntax but does not use `'\n'` as a separator.
......@@ -504,6 +504,50 @@ def concat(*args, separator=' '):
concat('a', 'b', 'c', separator='\n')
```
##
<!-- parameter rules -->
What does the signature of the following function mean?
```py
def fn(a, b, /, c, d=2, *, e, f=3, **kwargs):
pass
```
. . .
- `/`: `a` and `b` are [positional only arguments](https://docs.python.org/3/faq/programming.html#what-does-the-slash-in-the-parameter-list-of-a-function-mean). This means that we cannot pass `a` and `b` using their names.
- `c`, `d` are positional or keyword arguments
- `*` signs the end of positional arguments and the beginning of keyword-only arguments. It will throw the positional arguments away which are not addressed by a name.
- `2` and `3` are default arguments for `d` and `f`, so these are optional arguments
- `e`, `f` and `kwargs` are keyword-only arguments.
##
What could be the motivation for `/`, in other words positional only arguments?
. . .
From [Why use positional only arguments – Stackoverflow](https://stackoverflow.com/a/58478721):
> ...
> Some functions don't have any natural name to assign their argument to. Take the int constructor. int(x="3") is no more readable than int("3"). Positional-only arguments allow names that have no inherent meaning to be considered implementation details rather than part of the public API of the module
> ...
Code is written for humans. Providing a name could make a function more unreadable, especially in [mathematical functions](https://docs.python.org/3/library/math.html)
```py
import math; math.ceil?
# outputs
# Signature: math.ceil(x, /)
# Docstring:
# Return the ceiling of x as an Integral.
#
# This is the smallest integer >= x.
...
```
# parameter ordering
##
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment