Forked from jonathonw/gist:dac88b715aca4e4f1b2f513e1feaa102
Created
January 7, 2018 21:19
-
-
Save brianherman/4fd8b9faf2d193777b2f881648961600 to your computer and use it in GitHub Desktop.
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": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Start\n", | |
"Hello World\n", | |
"End\n", | |
"***\n", | |
"Foobar\n", | |
"Moo\n" | |
] | |
} | |
], | |
"source": [ | |
"class SomeClass:\n", | |
" def my_decorator(self, wrapped_fn):\n", | |
" def inner(*args, **kwargs):\n", | |
" print(\"Start\")\n", | |
" wrapped_fn(*args, **kwargs)\n", | |
" print(\"End\")\n", | |
" \n", | |
" return inner\n", | |
" \n", | |
" def decorator_with_params(self, param):\n", | |
" def real_decorator(function):\n", | |
" def inner(*args, **kwargs):\n", | |
" print(param)\n", | |
" function(*args, **kwargs)\n", | |
" return inner\n", | |
" return real_decorator\n", | |
" \n", | |
"my_instance = SomeClass()\n", | |
"\n", | |
"@my_instance.my_decorator\n", | |
"def do_a_thing():\n", | |
" print(\"Hello World\")\n", | |
" \n", | |
"do_a_thing()\n", | |
"\n", | |
"print(\"***\")\n", | |
"\n", | |
"@my_instance.decorator_with_params(\"Foobar\")\n", | |
"def do_another_thing():\n", | |
" print(\"Moo\")\n", | |
" \n", | |
"do_another_thing()\n", | |
" " | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment