-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Update Concept] functions, and complete function arguments #3067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
87 commits
Select commit
Hold shift + click to select a range
a93b986
Update introduction.md
bobahop fd486b0
Update introduction.md
bobahop 5587a28
Update about.md
bobahop 0f71889
Update concepts/functions/introduction.md
bobahop 64b3ac3
Update concepts/functions/introduction.md
bobahop 2e37714
Update concepts/functions/introduction.md
bobahop e089052
Update concepts/functions/introduction.md
bobahop 1e03d60
Update concepts/functions/introduction.md
bobahop 1e8ec3d
Update concepts/functions/introduction.md
bobahop 70eecf4
Update concepts/functions/introduction.md
bobahop 3806262
Update concepts/functions/introduction.md
bobahop 1060f09
Update introduction.md
bobahop e7a6cf6
Update introduction.md
bobahop 46ecaf8
Update about.md
bobahop 4df39f3
Update about.md
bobahop 734508a
Update introduction.md
bobahop 66fb45f
Update about.md
bobahop d850dd5
Update about.md
bobahop dfc1d56
Update about.md
bobahop 1b9bee6
Update introduction.md
bobahop 21f83dd
Update about.md
bobahop e7c61fc
Update about.md
bobahop ff20c55
Update about.md
bobahop 925c354
Update introduction.md
bobahop 33b6904
Update introduction.md
bobahop 62a440d
Update introduction.md
bobahop 8427ad9
Update about.md
bobahop 561a937
Update about.md
bobahop 246d58d
Update introduction.md
bobahop 60984bc
Update about.md
bobahop f867c43
Update concepts/function-arguments/about.md
bobahop 2a2d3cb
Update concepts/functions/about.md
bobahop c3b0f86
Update concepts/functions/about.md
bobahop 0d0a366
Update concepts/functions/about.md
bobahop 86e8cd0
Update about.md
bobahop 5576f5b
Update introduction.md
bobahop 364163e
Update about.md
bobahop 07d418d
Update concepts/functions/about.md
bobahop 81c759b
Update about.md
bobahop 7293efb
Update introduction.md
bobahop e437d98
Update about.md
bobahop bd20e5f
Update introduction.md
bobahop 9b965e5
Update about.md
bobahop 545afcb
Update about.md
bobahop 828f88c
Update concepts/functions/introduction.md
bobahop 4709ff9
Update concepts/function-arguments/about.md
bobahop 6c0f2ac
Update concepts/function-arguments/about.md
bobahop 0824e3b
Update concepts/function-arguments/about.md
bobahop f60c935
Update concepts/function-arguments/about.md
bobahop e6e0ff1
Update introduction.md
bobahop 936a1ea
Update about.md
bobahop 3711b99
Update about.md
bobahop ca50b61
Update concepts/function-arguments/about.md
bobahop 679bf40
Update concepts/function-arguments/about.md
bobahop 90fe571
Update concepts/function-arguments/about.md
bobahop 51a72b0
Update about.md
bobahop aab63a3
Update concepts/function-arguments/about.md
bobahop 49363a3
Update concepts/function-arguments/about.md
bobahop b937f6c
Update about.md
bobahop 5d1d01d
Update about.md
bobahop 62046eb
Apply suggestions from code review
BethanyG 88488db
Update concepts/function-arguments/about.md
bobahop 0035b87
Update concepts/function-arguments/about.md
bobahop e15fe1f
Update concepts/function-arguments/about.md
bobahop c1de003
Update about.md
bobahop b317024
Update about.md
bobahop 79b40c0
Update about.md
bobahop 69b3ce5
Update about.md
bobahop bfbd4f5
Update about.md
bobahop 09a8adb
Update introduction.md
bobahop 7c0737b
Update about.md
bobahop 1e023f0
Update concepts/function-arguments/about.md
bobahop e6652ea
Update concepts/function-arguments/about.md
bobahop 3a9cf6d
Update concepts/function-arguments/about.md
bobahop 836440a
Update concepts/function-arguments/about.md
bobahop 39c2649
Update concepts/function-arguments/about.md
bobahop 2d8902b
Update concepts/function-arguments/about.md
bobahop 4b002f8
Update concepts/function-arguments/about.md
bobahop c8339a4
Update about.md
bobahop 9bcc2d8
Update about.md
bobahop cd424c2
Update about.md
bobahop dc68fd1
Update concepts/functions/about.md
bobahop 27868f9
Update concepts/functions/about.md
bobahop 828ec8b
Update concepts/functions/about.md
bobahop 23132b3
Update concepts/functions/about.md
bobahop 0c71bda
Update concepts/functions/about.md
bobahop d769e51
Update about.md
bobahop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,295 @@ | ||
#TODO: Add about for this concept. | ||
# About | ||
|
||
For the basics on function arguments, please see the [function concept][function concept]. | ||
|
||
## Parameter Names | ||
|
||
Paramater names, like variable names, must start with a letter or underscore and may contain letters, underscores, or numbers. | ||
Parameter names should not contain spaces or punctuation. | ||
|
||
## Positional Arguments | ||
|
||
Positional arguments are values passed to a function in the same order as the parameters which bind to them. | ||
Positional arguments can optionally be passed by using their parameter name. | ||
|
||
Following is an example of positional arguments being passed by position and by their parameter name: | ||
|
||
```python | ||
>>> def concat(greeting, name): | ||
... return f"{greeting}{name}" | ||
... | ||
# Passing data to the function by position. | ||
>>> print(concat("Hello, ", "Bob")) | ||
bobahop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Hello, Bob | ||
... | ||
# Passing data to the function using the parameter name. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
bobahop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Hello, Bob | ||
|
||
``` | ||
|
||
The first call to `concat` passes the arguments by position. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The second call to `concat` passes the arguments by name, allowing their positions to be changed. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note that positional arguments cannot follow keyword arguments. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This | ||
|
||
```python | ||
>>> print(concat(greeting="Hello, ", "Bob")) | ||
``` | ||
|
||
results in this error: | ||
|
||
``` | ||
SyntaxError: positional argument follows keyword argument | ||
``` | ||
|
||
Requiring positional-only arguments for function calls can be done through the use of the `/` operator in the parameter list. | ||
|
||
|
||
Following is an example of positional-only arguments: | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
# Parameters showing a position-only operator. | ||
>>> def concat(greeting, name, /): | ||
... return f"{greeting}{name}" | ||
|
||
... | ||
>>> print(concat("Hello, ", "Bob")) | ||
Hello, Bob | ||
... | ||
# Call to the function using keyword arguments. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
Traceback (most recent call last): | ||
print(concat(name="Bob", greeting="Hello, ")) | ||
TypeError: concat() got some positional-only arguments passed as keyword arguments: 'greeting, name' | ||
|
||
|
||
``` | ||
|
||
## Keyword Arguments | ||
|
||
Keyword arguments use the parameter name when calling a function. | ||
Keyword arguments can optionally be referred to by position. | ||
|
||
Following is an example of keyword arguments being referred to by their parameter name and by position: | ||
|
||
```python | ||
>>> def concat(greeting, name): | ||
... return f"{greeting}{name}" | ||
... | ||
# Function call using parameter names as argument keywords. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
Hello, Bob | ||
... | ||
# Function call with positional data as arguments. | ||
>>> print(concat("Hello, ", "Bob")) | ||
Hello, Bob | ||
|
||
``` | ||
|
||
Requiring keyword-only arguments for function calls can be done through the use of the `*` operator in the parameter list. | ||
|
||
|
||
Following is an example of keyword-only arguments: | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
# Function definition requiring keyword-only arguments. | ||
>>> def concat(*, greeting, name): | ||
... return f"{greeting}{name}" | ||
... | ||
# Keyword arguments can be in an arbitrary order. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
Hello, Bob | ||
... | ||
# Calling the function with positional data raises an error. | ||
>>> print(concat("Hello, ", "Bob")) | ||
Traceback (most recent call last): | ||
print(concat("Hello, ", "Bob")) | ||
TypeError: concat() takes 0 positional arguments but 2 were given | ||
|
||
|
||
``` | ||
|
||
bobahop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Default Argument Values | ||
|
||
Default values for one or more arguments can be supplied in the parameter list. | ||
This allows the function to be called with _fewer_ arguments if needed. | ||
Default values can be overridden by calling the function with a new argument value, overriding the default: | ||
|
||
```python | ||
# Function with default argument values. | ||
>>> def concat(greeting, name="you", punctuation="!"): | ||
... return f"{greeting}, {name}{punctuation}" | ||
... | ||
>>> print(concat("Hello")) | ||
Hello, you! | ||
|
||
# Overriding the default values | ||
>>> print(concat("Hello", name="Bob", punctuation=".")) | ||
Hello, Bob. | ||
bobahop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
## Positional or Keyword Arguments | ||
|
||
Arguments can be positional or keyword if neither the `/` nor `*` operators are used in the parameter definitions. | ||
Alternately, the positional-or-keyword arguments can be placed between the positional-only parameters on the left and the keyword-only parameters on the right. | ||
|
||
Following is an example of positional-only, positional-or-keyword, and keyword-only arguments: | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
# Position-only argument followed by position-or-keyword, followed by keyword-only. | ||
>>> def concat(greeting, /, name, *, ending): | ||
... return f"{greeting}{name}{ending}" | ||
... | ||
>>> print(concat("Hello, ", "Bob", ending="!")) | ||
Hello, Bob! | ||
>>> print(concat("Hello, ", name="Bob", ending="!")) | ||
Hello, Bob! | ||
... | ||
>>> print(concat(greeting="Hello, ", name="Bob", ending="!")) | ||
Traceback (most recent call last): | ||
print(concat(greeting="Hello, ", name="Bob", ending="!")) | ||
TypeError: concat() got some positional-only arguments passed as keyword arguments: 'greeting' | ||
|
||
``` | ||
|
||
## `*args` | ||
|
||
Code examples will often use a function definition something like the following: | ||
|
||
```python | ||
def my_function(*args, **kwargs): | ||
# code snipped | ||
|
||
``` | ||
|
||
`*args` is a two-part name that represents a `tuple` with an indefinite number of separate positional arguments, also known as a [`variadic argument`][variadic argument]. | ||
`args` is the name given to the `tuple` of arguments, but it could be any other valid Python name, such as `my_args`, `arguments`, etc. | ||
The `*` is the operator which transforms the group of separate arguments into a [`tuple`][tuple]. | ||
|
||
~~~~exercism/note | ||
If you have ever [unpacked a tuple][unpack a tuple] you may find the `*` in `*args` to be confusing. | ||
The `*` in a _parameter_ definition, instead of unpacking a tuple, converts one or more positional arguments _into_ a tuple. | ||
|
||
We say that the `*` operator is [overloaded], as it has different behavior in different contexts. | ||
For instance, `*` is used for multiplication, it is used for unpacking, and it is used to define an arbitrary number of positional parameters. | ||
~~~~ | ||
|
||
Since a tuple can be iterated, `args` can be passed to any other function which takes an iterable. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Although `*args` is commonly juxtaposed with `**kwargs`, it doesn't have to be. | ||
|
||
Following is an example of an arbitrary amount of values being passed to a function: | ||
|
||
```python | ||
|
||
>>> def add(*args): | ||
# args is passed to the sum function, which takes an iterable | ||
... return sum(args) | ||
... | ||
>>> print(add(1, 2, 3)) | ||
6 | ||
``` | ||
|
||
If `*args` follows one or more positional arguments, then `*args` will be what is left over after the positional arguments. | ||
|
||
Following is an example of an arbitrary amount of values being passed to a function after a positional argument: | ||
|
||
```python | ||
|
||
>>> def add(first, *args): | ||
# first will be 1, leaving the values 2 and 3 in *args | ||
... return first + sum(args) | ||
... | ||
>>> print(add(1, 2, 3)) | ||
6 | ||
``` | ||
|
||
If one or more default arguments are defined after `*args` they are separate from the `*args` values. | ||
|
||
To put it all together is an example of an arbitrary amount of values being passed to a function that also has a positional argument and a default argument: | ||
|
||
```python | ||
|
||
>>> def add(first, *args, last=0): | ||
... return first + sum(args) + last | ||
... | ||
>>> print(add(1, 2, 3)) | ||
6 | ||
>>> print(add(1, 2, 3, last=4)) | ||
10 | ||
# This uses the unpacking operator * to separate the list elements into positional arguments. | ||
# It does not have the same behavior as the * in *args. | ||
>>> print(add(*[1, 2, 3])) | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
6 | ||
|
||
``` | ||
|
||
Note that when an argument is already in an iterable, such as a tuple or list, it needs to be unpacked before being passed to a function that takes an arbitrary amount of separate arguments. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This is accomplished by using `*`, which is the [unpacking operator][unpacking operator]. | ||
|
||
`*` in this context _unpacks_ the container into its separate elements which are then transformed by `*args` into a tuple. | ||
Where there are only positional arguments, the unpacking action must result in the same number of arguments as there are formal parameters. | ||
|
||
Without unpacking the list passed into `add`, the program would error. | ||
|
||
```python | ||
>>>> def add(first, *args, last=0): | ||
... return first + sum(args) + last | ||
... | ||
>>>> print(add([1, 2, 3])) | ||
Traceback (most recent call last): | ||
print(add([1, 2, 3])) | ||
return first + sum(args) + last | ||
TypeError: can only concatenate list (not "int") to list | ||
|
||
``` | ||
|
||
## `**kwargs` | ||
|
||
`**kwargs` is a two-part name that represents an indefinite number of separate [key-value pair][key-value] arguments. | ||
`kwargs` is the name of the group of arguments and could be any other name, such as `my_args`, `arguments`, etc. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The `**` transforms the group of named arguments into a [`dictionary`][dictionary] of `{argument name: argument value}` pairs. | ||
|
||
Since a dictionary can be iterated, `kwargs` can be passed to any other function which takes an iterable. | ||
Although `**kwargs` is commonly juxtaposed with `*args`, it doesn't have to be. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Following is an example of an arbitrary amount of key-value pairs being passed to a function: | ||
|
||
```python | ||
>>> def add(**kwargs): | ||
... return sum(kwargs.values()) | ||
... | ||
>>> print(add(one=1, two=2, three=3)) | ||
6 | ||
``` | ||
|
||
Note that the `dict.values()` method is called to iterate through the `kwargs` dictionary values. | ||
|
||
When iterating a dictionary the default is to iterate the keys. | ||
|
||
Following is an example of an arbitrary amount of key-value pairs being passed to a function that then iterates over `kwargs.keys()`: | ||
|
||
```python | ||
>>> def concat(**kwargs): | ||
# Join concatenates the key names from `kwargs.keys()` | ||
... return " ".join(kwargs) | ||
... | ||
>>> print(concat(one=1, two=2, three=3)) | ||
one two three | ||
|
||
``` | ||
|
||
|
||
[default arguments]: https://www.geeksforgeeks.org/default-arguments-in-python/ | ||
[dictionary]: https://www.w3schools.com/python/python_dictionaries.asp | ||
[function concept]: ../functions/about.md | ||
[key-value]: https://www.pythontutorial.net/python-basics/python-dictionary/ | ||
[overloaded]: https://www.geeksforgeeks.org/operator-overloading-in-python/ | ||
[tuple]: https://www.w3schools.com/python/python_tuples.asp | ||
[unpack a tuple]: https://www.geeksforgeeks.org/unpacking-a-tuple-in-python/ | ||
[unpacking operator]: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists | ||
bobahop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[variadic argument]: https://en.wikipedia.org/wiki/Variadic_function |
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,73 @@ | ||
#TODO: Add introduction for this concept. | ||
# Introduction | ||
|
||
For the basics on function arguments, please see the [function concept][function concept]. | ||
|
||
## Parameter Names | ||
|
||
Paramater names, like variable names, must start with a letter or underscore and may contain letters, underscores, or numbers. | ||
Parameter names should not contain spaces or punctuation. | ||
|
||
## Positional Arguments | ||
|
||
Positional arguments are values passed to a function in the same order as the parameters which bind to them. | ||
Positional arguments can optionally be passed by using their parameter name. | ||
|
||
Following is an example of positional arguments being passed by position and by their parameter name: | ||
|
||
```python | ||
>>> def concat(greeting, name): | ||
... return f"{greeting}{name}" | ||
... | ||
# Passing data to the function by position. | ||
>>> print(concat("Hello, ", "Bob")) | ||
Hello, Bob | ||
... | ||
# Passing data to the function using the parameter name. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
Hello, Bob | ||
|
||
``` | ||
|
||
The first call to concat passes the arguments by position. | ||
The second call to concat passes the arguments by name, allowing their positions to be changed. | ||
|
||
Note that positional arguments cannot follow keyword arguments. | ||
|
||
This | ||
|
||
```python | ||
>>> print(concat(greeting="Hello, ", "Bob")) | ||
``` | ||
|
||
results in this error: | ||
|
||
``` | ||
SyntaxError: positional argument follows keyword argument | ||
``` | ||
|
||
## Keyword Arguments | ||
|
||
Keyword arguments use the parameter name when calling a function. | ||
Keyword arguments can optionally be referred to by position. | ||
|
||
Following is an example of keyword arguments being referred to by their parameter name and by position: | ||
|
||
```python | ||
>>> def concat(greeting="Hello, ", name="you"): | ||
... return f"{greeting}{name}" | ||
... | ||
# Function call using parameter names as argument keywords. | ||
>>> print(concat(name="Bob", greeting="Hello, ")) | ||
Hello, Bob | ||
... | ||
# Function call with positional data as arguments. | ||
>>> print(concat("Hello, ", name="Bob")) | ||
Hello, Bob | ||
>>> print(concat()) | ||
Hello, you | ||
|
||
``` | ||
|
||
[default arguments]: https://www.geeksforgeeks.org/default-arguments-in-python/ | ||
[function concept]: ../functions/about.md | ||
[parameters]: https://www.codecademy.com/learn/flask-introduction-to-python/modules/learn-python3-functions/cheatsheet |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.