Skip to content

Commit

Permalink
Add Python/keyword-only-arguments.md
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Lane <[email protected]>
  • Loading branch information
jatonline and richard-lane committed Aug 19, 2024
1 parent 9db19ec commit 28d8840
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Python/keyword-only-arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Function arguments that can only be used a keywords, not positional arguments

You can define a keyword-only function in python by using a `*` (everything afterwards will be keyword-only). This can make the meaning of function arguments much clearer, compare:

```python
my_function(my_data, False, 4, 16, None)
```

and:

```python
my_function(my_data, shuffle=False, num_workers=4, batch_size=16, output_file=None)
```

You'd define this as:

```python
def my_function(my_data, *, shuffle, num_workers, batch_size, output_file):
...
```

You can also have [positional-only arguments](https://peps.python.org/pep-0570/), but you probably don't need to do that.

Via: [Richard](https://github.com/richard-lane)

0 comments on commit 28d8840

Please sign in to comment.