Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytreat-practicals-2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tom Nichols
pytreat-practicals-2020
Commits
02e84cb3
Commit
02e84cb3
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Little more work on args prac
parent
48903cbb
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
advanced_topics/function_inputs_and_outputs.ipynb
+60
-4
60 additions, 4 deletions
advanced_topics/function_inputs_and_outputs.ipynb
advanced_topics/function_inputs_and_outputs.md
+34
-2
34 additions, 2 deletions
advanced_topics/function_inputs_and_outputs.md
with
94 additions
and
6 deletions
advanced_topics/function_inputs_and_outputs.ipynb
+
60
−
4
View file @
02e84cb3
...
...
@@ -180,10 +180,66 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"> Pitfall: mutable argument values\n",
"\n",
"\n",
"You can see here that"
"__WARNING:__ _Never_ define a function with a mutable default value, such as a\n",
"`list`, `dict` or other non-primitive type. Let's see what happens when we do:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def badfunc(a=[]):\n",
" a.append('end of sequence')\n",
" output = ', '.join([str(elem) for elem in a])\n",
" print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this function, all is well and good if we pass in our own value for `a`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"badfunc([1, 2, 3, 4])\n",
"badfunc([2, 4, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But what happens when we let `badfunc` use the default value for `a`?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"badfunc()\n",
"badfunc()\n",
"badfunc()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This happens because default argument values are created when the function is\n",
"defined, and will persist for the duration of your program. So in this\n",
"example, the default value for `a`, a Python `list`, gets created when\n",
"`badfunc` is defined, and hangs around for the lifetime of the `badfunc`\n",
"function!"
]
}
],
...
...
%% Cell type:markdown id: tags:
# Function inputs and outputs
In Python, arguments to a function can be specified in two different ways - by
using _positional_ arguments, or by using _keyword_ arguments.
## Positional arguments
Let's say we have a function that looks like this
%% Cell type:code id: tags:
```
def myfunc(a, b, c):
print('First argument: ', a)
print('Second argument:', b)
print('Third argument: ', c)
```
%% Cell type:markdown id: tags:
If we call this function like so:
%% Cell type:code id: tags:
```
myfunc(1, 2, 3)
```
%% Cell type:markdown id: tags:
The values
`1`
,
`2`
and
`3`
get assigned to arguments
`a`
,
`b`
, and
`c`
respectively, based on the position in which they are passed.
Python allows us to pass positional arguments into a function from a sequence,
using the star (
`*`
) operator. So we could store our arguments in a list or
tuple, and then pass the list straight in:
%% Cell type:code id: tags:
```
args = [3, 4, 5]
myfunc(*args)
```
%% Cell type:markdown id: tags:
You can think of the star operator as 'unpacking' the contents of the
sequence.
## Keyword arguments
Using keyword arguments allows us to pass arguments to a function in any order
we like. We could just as easily call our
`myfunc`
function like so, and get
the same result that we did earlier when using positional arguments:
%% Cell type:code id: tags:
```
myfunc(c=3, b=2, a=1)
```
%% Cell type:markdown id: tags:
Python has another operator - the double-star (
`**`
), which will unpack
keyword arguments from
`dict`
. For example:
%% Cell type:code id: tags:
```
kwargs = {'a' : 4, 'b' : 5, 'c' : 6}
myfunc(**kwargs)
```
%% Cell type:markdown id: tags:
## Combining positional and keyword arguments
In fact, we can use both of these techniques at once, like so:
%% Cell type:code id: tags:
```
args = (100, 200)
kwargs = {'c' : 300}
myfunc(*args, **kwargs)
```
%% Cell type:markdown id: tags:
## Default argument values
Function arguments can be given default values, like so:
%% Cell type:code id: tags:
```
myfunc(a=1, b=2, c=3):
print('First argument: ', a)
print('Second argument:', b)
print('Third argument: ', c)
```
%% Cell type:markdown id: tags:
Now we can call
`myfunc`
, only passing the arguments that we need to. The
arguments which are unspecified in the function call will be assigned their
default value:
%% Cell type:code id: tags:
```
myfunc()
myfunc(10)
myfunc(10, b=30)
myfunc(c=300)
```
%% Cell type:markdown id: tags:
> Pitfall: mutable argument values
__WARNING:__
_Never_ define a function with a mutable default value, such as a
`list`
,
`dict`
or other non-primitive type. Let's see what happens when we do:
%% Cell type:code id: tags:
```
def badfunc(a=[]):
a.append('end of sequence')
output = ', '.join([str(elem) for elem in a])
print(output)
```
%% Cell type:markdown id: tags:
With this function, all is well and good if we pass in our own value for
`a`
:
%% Cell type:code id: tags:
```
badfunc([1, 2, 3, 4])
badfunc([2, 4, 6])
```
%% Cell type:markdown id: tags:
But what happens when we let
`badfunc`
use the default value for
`a`
?
%% Cell type:code id: tags:
```
badfunc()
badfunc()
badfunc()
```
%% Cell type:markdown id: tags:
You can see here that
This happens because default argument values are created when the function is
defined, and will persist for the duration of your program. So in this
example, the default value for
`a`
, a Python
`list`
, gets created when
`badfunc`
is defined, and hangs around for the lifetime of the
`badfunc`
function!
...
...
This diff is collapsed.
Click to expand it.
advanced_topics/function_inputs_and_outputs.md
+
34
−
2
View file @
02e84cb3
...
...
@@ -106,7 +106,39 @@ myfunc(c=300)
```
> Pitfall: mutable argument values
__WARNING:__
_Never_ define a function with a mutable default value, such as a
`list`
,
`dict`
or other non-primitive type. Let's see what happens when we do:
You can see here that
```
def badfunc(a=[]):
a.append('end of sequence')
output = ', '.join([str(elem) for elem in a])
print(output)
```
With this function, all is well and good if we pass in our own value for
`a`
:
```
badfunc([1, 2, 3, 4])
badfunc([2, 4, 6])
```
But what happens when we let
`badfunc`
use the default value for
`a`
?
```
badfunc()
badfunc()
badfunc()
```
This happens because default argument values are created when the function is
defined, and will persist for the duration of your program. So in this
example, the default value for
`a`
, a Python
`list`
, gets created when
`badfunc`
is defined, and hangs around for the lifetime of the
`badfunc`
function!
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment