Skip to content

Instantly share code, notes, and snippets.

@fuzzy-focus
Created October 28, 2016 08:02
Show Gist options
  • Save fuzzy-focus/23ca768fef666b62cc0e30e8bcda7c61 to your computer and use it in GitHub Desktop.
Save fuzzy-focus/23ca768fef666b62cc0e30e8bcda7c61 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Deine Frage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kurze Funktion für bessere Anzeige"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def fun_print(f, x):\n",
" try:\n",
" print('{}({}) = {}'.format(f.__name__,x,f(x)))\n",
" except Exception as e:\n",
" print('{}({}) raised exception \"{}\"'.format(f.__name__,x,e))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktion mit Decorator"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import math\n",
"from functools import wraps\n",
"def argument_check(f):\n",
" @wraps(f)\n",
" def deco_deco(x):\n",
" if x < 0:\n",
" raise Exception('argument invalid')\n",
" else:\n",
" return f(x)\n",
" return deco_deco\n",
" \n",
"@argument_check\n",
"def square_root(i):\n",
" return math.sqrt(i)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"square_root(9) = 3.0\n",
"square_root(-1) raised exception \"argument invalid\"\n"
]
}
],
"source": [
"fun_print(square_root,9)\n",
"fun_print(square_root,-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktion ohne Decorator, stattdessen interner function call"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def argument_check2(x):\n",
" if x < 0:\n",
" raise Exception('argument invalid')\n",
"\n",
"def square_root2(i):\n",
" check = argument_check2(i)\n",
" return math.sqrt(i)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"square_root2(9) = 3.0\n",
"square_root2(-1) raised exception \"argument invalid\"\n"
]
}
],
"source": [
"fun_print(square_root2,9)\n",
"fun_print(square_root2,-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diskussion\n",
"Du hast also recht, da kommt schon das selbe raus. Und in diesem Fall wäre der Vorteil eines **decorators** jetzt auch nicht riesig.\n",
"\n",
"Bei **decorator**s gehts grundsätzlich da rum irgendwelche Prüfungen die du durchführen möchtest, oder irgendwelche anderen Zusatzfunktionen (Caching, Registration, Routing, …) modular definieren zu können um sie dann auf übersichtliche Art und Weise einer (oder mehreren) Funktion zuordnen zu können.\n",
"\n",
"Grade bei Argument-Checks ists ganz gut zu verstehen. \n",
"Du hast eine Funktion die irgendwas macht - meinetwegen *write_value_to_db*, einen Wert in eine Datenbank schreiben.\n",
"Jetzt möchtest du noch festlegen dass dieser Wert nur ganzzahlig sein darf und zwischen 5 und 23 liegt.\n",
"Das ist aber nicht teil der Aufgabe von *write_value_to_db*, welches nur genau tun soll was der Name sagt.\n",
"Also Arbeitest du mit mehreren **decorator**s.\n",
"Du kannst die Funktion danach noch ganz normal sehen und erkennen was sie macht, ohne das du nen riesigen Block mit argument-checks drin hast.\n",
"Weil die eben ihre eigene Funktionalität haben, welche ausgelagert ist.\n",
"\n",
"Du musst **decorator**s nicht einsetzen, von der Funktion her bieten se dir (in den meisten Fällen) keine Vorteile.\n",
"Aber sie machen deinen Code lesbarer, übersichtlicher und modularer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment