Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
nam
mop
Commits
23b0e421
Commit
23b0e421
authored
Aug 18, 2021
by
Jochen Schulz
Browse files
update dekoratoren
parent
9c2a0d9b
Pipeline
#221503
failed with stage
in 7 minutes and 5 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/Dekoratoren/dekoratoren_lecture.md
View file @
23b0e421
...
...
@@ -3,8 +3,10 @@ jupytext:
text_representation
:
extension
:
.md
format_name
:
myst
format_version
:
0.13
jupytext_version
:
1.10.3
kernelspec
:
display_name
:
Python
3
display_name
:
Python 3
(ipykernel)
language
:
python
name
:
python3
---
...
...
@@ -36,7 +38,7 @@ D.h. nach der ersten Definition von `some_function` wird `some_decorator` ausgef
*Beispiel*
: Decorator, der Funktionsargumente und Rückgabewerte ausgibt (z.B. zum Debugging):
```
{code-cell}
```
{code-cell}
ipython3
def print_args_and_result(func):
def new_func(*args):
print("-- {} called with arguments {}".format(func.__name__, args))
...
...
@@ -54,7 +56,7 @@ Hierbei ist
das Tupel aller Argumente der Funktion
`func`
.
```
{code-cell}
```
{code-cell}
ipython3
@print_args_and_result
def print_sum(a, b):
"""
...
...
@@ -71,17 +73,17 @@ print_sum(3, 4)
Die neue Funktion übernimmt nicht die Metadaten der ursprünglichen Funktion, z.B. Funktionsnamen oder Docstring:
```
{code-cell}
```
{code-cell}
ipython3
print_sum.__name__
```
```
{code-cell}
```
{code-cell}
ipython3
print(print_sum.__doc__)
```
Um das zu erreichen, empfiehlt es sich, die neue Funktion selbst mit
`functools.wraps`
zu dekorieren:
```
{code-cell}
```
{code-cell}
ipython3
from functools import wraps
def print_args_and_result(func):
...
...
@@ -94,7 +96,7 @@ def print_args_and_result(func):
return new_func
```
```
{code-cell}
```
{code-cell}
ipython3
@print_args_and_result
def print_sum(a, b):
"""
...
...
@@ -117,11 +119,11 @@ def print_sum(a, b):
print_sum(3, 4)
```
```
{code-cell}
```
{code-cell}
ipython3
print_sum.__name__
```
```
{code-cell}
```
{code-cell}
ipython3
print(print_sum.__doc__)
```
...
...
@@ -148,7 +150,7 @@ functools.lru_cache(maxsize=None)
Erinnerung: Die naive Implementierung der Fibonacci-Folge
```
{code-cell}
```
{code-cell}
ipython3
def fib(n):
if n <= 1:
return n
...
...
@@ -158,13 +160,13 @@ def fib(n):
ist extrem langsam, da viele der Folgenglieder mehrfach berechnet werden müssen.
```
{code-cell}
```
{code-cell}
ipython3
%timeit fib(30)
```
Mit
`lru_cache`
(der Einfachheit halber ohne
`maxsize`
):
```
{code-cell}
```
{code-cell}
ipython3
from functools import lru_cache
@lru_cache(maxsize=None)
...
...
@@ -175,6 +177,6 @@ def fib(n):
return fib(n - 1) + fib(n - 2)
```
```
{code-cell}
```
{code-cell}
ipython3
%timeit fib(30)
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment