Last active
October 17, 2021 13:13
-
-
Save RenSys/3fbe82cb67478577967c85d32d2ab544 to your computer and use it in GitHub Desktop.
[Decorators function] #builtin #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Decorators \nA decorator is any callable Python object that is used to modify a function, method or class definition. A decorator is passed the original object being defined and returns a modified object, which is then bound to the name in the definition. Python decorators were inspired in part by Java annotations, and have a similar syntax; the decorator syntax is pure syntactic sugar, using @ as the keyword." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Use Cases" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Decorators are python synati csugar for embeddeding functions within functions to make it easier to maintain (i.e. scalable and a single place to edit) common features shared across code. \n\nA practical example is [debug logger](https://gist.github.com/dabbe2a5432cb4083c51ffea8d71eab7)\n" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Step 1 - Declare the decorator function" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "def logger(func):\n \"\"\"\n :return: func\n \"\"\"\n print('logger added to func:{}'.format(func.__name__))\n\n def wrapper(*args, **kwargs):\n print('calling: {}'.format(func.__name__))\n return func(*args, **kwargs)\n return wrapper\n\n\n", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Step 2 - Bind the decorator the function\nThe print_hello_world function is modified in order to bind append the logger function when called. \n\nAt this point, the logger function is called, passing the print_hello_world. The logger function will then execute all code (including the def line only) except the return keyword, resulting in the print statement to be sent to the console i.e. 'logger added to func'" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "@logger\ndef print_hello_world(x, y):\n print('x:{}, y:{}'.format(x, y))\n", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "logger added to func:print_hello_world\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "print_hello_world(x='karl', y = 'beigan')", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "calling: print_hello_world\nx:karl, y:beigan\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Standard - Decorators nested function calling", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment