Skip to content
Snippets Groups Projects
Commit 6042d8da authored by Nicola Spallanzani's avatar Nicola Spallanzani
Browse files

aggiunto notebook sui decoratori

parent b12cf386
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
![](images/title_intro_python.png)
%% 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.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment