-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python/keyword-only-arguments.md
Co-authored-by: Richard Lane <[email protected]>
- Loading branch information
1 parent
9db19ec
commit 28d8840
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |