Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
intro-python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Nicola Spallanzani
intro-python
Commits
6042d8da
Commit
6042d8da
authored
6 years ago
by
Nicola Spallanzani
Browse files
Options
Downloads
Patches
Plain Diff
aggiunto notebook sui decoratori
parent
b12cf386
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
16-Decorators.ipynb
+732
-0
732 additions, 0 deletions
16-Decorators.ipynb
with
732 additions
and
0 deletions
16-Decorators.ipynb
0 → 100644
+
732
−
0
View file @
6042d8da
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# functions are first class citizens"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## functions are first class citizens\n",
"\n",
"Functions as first class citizens means that, you can pass functions around just like other objects:\n",
"\n",
"- assign them to variables"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def foo():\n",
" print(foo.__name__)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo\n"
]
}
],
"source": [
"bar = foo # without the round brackets\n",
"bar()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## functions are first class citizens\n",
"\n",
"- pass them along as arguments"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def summa(a, b): return a+b\n",
"def sub(a, b): return a-b\n",
"def g(x, f): return f(x, x+1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g(2, summa)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g(2, sub)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## functions are first class citizens\n",
"\n",
"- store them in bigger data structures"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def foo():\n",
" pass\n",
"\n",
"def bar():\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"functions_list = [foo, bar] "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## functions are first class citizens\n",
"\n",
"- define them inside another function"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def foo():\n",
" def bar():\n",
" print(\"Hello!\")\n",
" return bar()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello!\n"
]
}
],
"source": [
"foo() "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## functions are first class citizens\n",
"\n",
"- return them from another function"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def foo():\n",
" def bar():\n",
" print(\"Hello!\")\n",
" return bar"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.foo.<locals>.bar()>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"foo()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello!\n"
]
}
],
"source": [
"f = foo()\n",
"f() "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# decorators"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators\n",
"\n",
"A decorator is a function (or a class) userd to chenge the functionality of another function."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def decorator_func(a_func):\n",
" def wrapper():\n",
" # add something betore\n",
" result = a_func()\n",
" # add something after\n",
" return result\n",
" return wrapper"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def decorator_func(a_func):\n",
" def wrapper():\n",
" print(\"Just before to run the function {}.\".format(a_func.__name__))\n",
" return a_func()\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def my_func():\n",
" print(\"My function need a decoration.\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Just before to run the function my_func.\n",
"My function need a decoration.\n"
]
}
],
"source": [
"decorated_func = decorator_func(my_func) \n",
"decorated_func() "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators\n",
"\n",
"If you need only the decorated function:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Just before to run the function my_func.\n",
"My function need a decoration.\n"
]
}
],
"source": [
"my_func = decorator_func(my_func)\n",
"my_func() "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"@decorator_func\n",
"def another_func():\n",
" print(\"Another function that need a decoration.\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Just before to run the function another_func.\n",
"Another function that need a decoration.\n"
]
}
],
"source": [
"another_func() "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators\n",
"\n",
"Function with arguments"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"@decorator_func\n",
"def summa(a, b):\n",
" return a + b"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"ename": "TypeError",
"evalue": "wrapper() takes 0 positional arguments but 2 were given",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-37-13b01c772a1b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msumma\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: wrapper() takes 0 positional arguments but 2 were given"
]
}
],
"source": [
"summa(3, 5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decrators"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def decorator_func(a_func):\n",
" def wrapper(*args, **kwargs):\n",
" print(\"Just before to run the function {}.\".format(a_func.__name__))\n",
" return a_func(*args, **kwargs)\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Just before to run the function summa.\n"
]
},
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@decorator_func\n",
"def summa(a, b):\n",
" return a + b\n",
"\n",
"summa(3, 5) "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## test (decorators)\n",
"\n",
"Write a module containing a decorator used for timing how long a function run.\n",
"\n",
"Hint: use the function `time()` from the module `time`."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators\n",
"\n",
"It is possible to apply many decorators to the same function.\n",
"\n",
"```python\n",
"@decorator1\n",
"@decorator2\n",
"def foo():\n",
" pass\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## decorators\n",
"\n",
"Since a decorator is a function, it is possible to pass argument to a decorator."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def decorator_arg(arg):\n",
" def decorator_func(a_func):\n",
" def wrapper(*args, **kwargs):\n",
" print(arg, \"Just before to run the function {}.\".format(a_func.__name__))\n",
" return a_func(*args, **kwargs)\n",
" return wrapper\n",
" return decorator_func"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LOG: Just before to run the function another_func.\n",
"Another function that need a decoration.\n"
]
}
],
"source": [
"@decorator_arg(\"LOG:\")\n",
"def another_func():\n",
" print(\"Another function that need a decoration.\")\n",
" \n",
"another_func() "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:

%% Cell type:markdown id: tags:
# functions are first class citizens
%% Cell type:markdown id: tags:
## functions are first class citizens
Functions as first class citizens means that, you can pass functions around just like other objects:
-
assign them to variables
%% Cell type:code id: tags:
```
python
def
foo
():
print
(
foo
.
__name__
)
```
%% Cell type:code id: tags:
```
python
bar
=
foo
# without the round brackets
bar
()
```
%% Output
foo
%% Cell type:markdown id: tags:
## functions are first class citizens
-
pass them along as arguments
%% Cell type:code id: tags:
```
python
def
summa
(
a
,
b
):
return
a
+
b
def
sub
(
a
,
b
):
return
a
-
b
def
g
(
x
,
f
):
return
f
(
x
,
x
+
1
)
```
%% Cell type:code id: tags:
```
python
g
(
2
,
summa
)
```
%% Output
5
%% Cell type:code id: tags:
```
python
g
(
2
,
sub
)
```
%% Output
-1
%% Cell type:markdown id: tags:
## functions are first class citizens
-
store them in bigger data structures
%% Cell type:code id: tags:
```
python
def
foo
():
pass
def
bar
():
pass
```
%% Cell type:code id: tags:
```
python
functions_list
=
[
foo
,
bar
]
```
%% Cell type:markdown id: tags:
## functions are first class citizens
-
define them inside another function
%% Cell type:code id: tags:
```
python
def
foo
():
def
bar
():
print
(
"
Hello!
"
)
return
bar
()
```
%% Cell type:code id: tags:
```
python
foo
()
```
%% Output
Hello!
%% Cell type:markdown id: tags:
## functions are first class citizens
-
return them from another function
%% Cell type:code id: tags:
```
python
def
foo
():
def
bar
():
print
(
"
Hello!
"
)
return
bar
```
%% Cell type:code id: tags:
```
python
foo
()
```
%% Output
<function __main__.foo.<locals>.bar()>
%% Cell type:code id: tags:
```
python
f
=
foo
()
f
()
```
%% Output
Hello!
%% Cell type:markdown id: tags:
# decorators
%% Cell type:markdown id: tags:
## decorators
A decorator is a function (or a class) userd to chenge the functionality of another function.
%% Cell type:code id: tags:
```
python
def
decorator_func
(
a_func
):
def
wrapper
():
# add something betore
result
=
a_func
()
# add something after
return
result
return
wrapper
```
%% Cell type:markdown id: tags:
## decorators
%% Cell type:code id: tags:
```
python
def
decorator_func
(
a_func
):
def
wrapper
():
print
(
"
Just before to run the function {}.
"
.
format
(
a_func
.
__name__
))
return
a_func
()
return
wrapper
```
%% Cell type:code id: tags:
```
python
def
my_func
():
print
(
"
My function need a decoration.
"
)
```
%% Cell type:code id: tags:
```
python
decorated_func
=
decorator_func
(
my_func
)
decorated_func
()
```
%% Output
Just before to run the function my_func.
My function need a decoration.
%% Cell type:markdown id: tags:
## decorators
If you need only the decorated function:
%% Cell type:code id: tags:
```
python
my_func
=
decorator_func
(
my_func
)
my_func
()
```
%% Output
Just before to run the function my_func.
My function need a decoration.
%% Cell type:code id: tags:
```
python
@decorator_func
def
another_func
():
print
(
"
Another function that need a decoration.
"
)
```
%% Cell type:code id: tags:
```
python
another_func
()
```
%% Output
Just before to run the function another_func.
Another function that need a decoration.
%% Cell type:markdown id: tags:
## decorators
Function with arguments
%% Cell type:code id: tags:
```
python
@decorator_func
def
summa
(
a
,
b
):
return
a
+
b
```
%% Cell type:code id: tags:
```
python
summa
(
3
,
5
)
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-13b01c772a1b> in <module>()
----> 1 summa(3, 5)
TypeError: wrapper() takes 0 positional arguments but 2 were given
%% Cell type:markdown id: tags:
## decrators
%% Cell type:code id: tags:
```
python
def
decorator_func
(
a_func
):
def
wrapper
(
*
args
,
**
kwargs
):
print
(
"
Just before to run the function {}.
"
.
format
(
a_func
.
__name__
))
return
a_func
(
*
args
,
**
kwargs
)
return
wrapper
```
%% Cell type:code id: tags:
```
python
@decorator_func
def
summa
(
a
,
b
):
return
a
+
b
summa
(
3
,
5
)
```
%% Output
Just before to run the function summa.
8
%% Cell type:markdown id: tags:
## test (decorators)
Write a module containing a decorator used for timing how long a function run.
Hint: use the function
`time()`
from the module
`time`
.
%% Cell type:markdown id: tags:
## decorators
It is possible to apply many decorators to the same function.
```
python
@decorator1
@decorator2
def
foo
():
pass
```
%% Cell type:markdown id: tags:
## decorators
Since a decorator is a function, it is possible to pass argument to a decorator.
%% Cell type:code id: tags:
```
python
def
decorator_arg
(
arg
):
def
decorator_func
(
a_func
):
def
wrapper
(
*
args
,
**
kwargs
):
print
(
arg
,
"
Just before to run the function {}.
"
.
format
(
a_func
.
__name__
))
return
a_func
(
*
args
,
**
kwargs
)
return
wrapper
return
decorator_func
```
%% Cell type:code id: tags:
```
python
@decorator_arg
(
"
LOG:
"
)
def
another_func
():
print
(
"
Another function that need a decoration.
"
)
another_func
()
```
%% Output
LOG: Just before to run the function another_func.
Another function that need a decoration.
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