- decoratorの復習
- wrapsってなに?
- その効果
Last active
July 24, 2017 02:25
-
-
Save ksomemo/58b1a8339bc7a0c12aaf to your computer and use it in GitHub Desktop.
decorator メモ
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": 1, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import mywraps" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "decorated func nowarps\n", | |
| "example1\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "mywraps.example1()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "decorated fun wraps\n", | |
| "ex2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "mywraps.example2()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "decorated func nowarps\n", | |
| "1 y2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "func = lambda x, y=\"y1\": print(x, y)\n", | |
| "mywraps.decorator_nowraps(func)(1, y=\"y2\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "attrs = [\n", | |
| " '__module__',\n", | |
| " '__annotations__',\n", | |
| " '__name__',\n", | |
| " '__doc__',\n", | |
| " '__dict__',\n", | |
| "]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from pprint import pprint\n", | |
| "def print_attrs(attrs, obj):\n", | |
| " pprint({attr: getattr(obj, attr, \"not exist\") for attr in attrs})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'__annotations__': {},\n", | |
| " '__dict__': {},\n", | |
| " '__doc__': None,\n", | |
| " '__module__': 'mywraps',\n", | |
| " '__name__': 'wrapper'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print_attrs(attrs, mywraps.example1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'__annotations__': {},\n", | |
| " '__dict__': {'__wrapped__': <function example2 at 0x00000000044CA8C8>},\n", | |
| " '__doc__': 'Docstr ex2',\n", | |
| " '__module__': 'mywraps',\n", | |
| " '__name__': 'example2'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print_attrs(attrs, mywraps.example2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'__main__'" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "__name__" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'__annotations__': {},\n", | |
| " '__dict__': {'__wrapped__': <function func_in_main1 at 0x00000000044F1620>},\n", | |
| " '__doc__': 'Docstr func main1',\n", | |
| " '__module__': '__main__',\n", | |
| " '__name__': 'func_in_main1'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "@mywraps.decorator_wraps\n", | |
| "def func_in_main1():\n", | |
| " \"\"\"Docstr func main1\"\"\"\n", | |
| " print('func main1')\n", | |
| "\n", | |
| "print_attrs(attrs, func_in_main1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "<function mywraps.example3>" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "mywraps.example3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "decorated func wraps and update\n", | |
| "ex3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "mywraps.example3()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'__annotations__': {},\n", | |
| " '__dict__': {'__wrapped__': <function example3 at 0x00000000044CAA60>},\n", | |
| " '__doc__': 'Docstr ex3',\n", | |
| " '__module__': 'mywraps',\n", | |
| " '__name__': 'example3'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print_attrs(attrs, mywraps.example3)" | |
| ] | |
| } | |
| ], | |
| "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.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
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
| from functools import ( | |
| wraps, | |
| update_wrapper | |
| ) | |
| # http://docs.python.jp/3.5/library/functools.html#functools.update_wrapper | |
| def decorator_nowraps(f): | |
| def wrapper(*args, **kwds): | |
| print('decorated func nowarps') | |
| return f(*args, **kwds) | |
| return wrapper | |
| def decorator_wraps(f): | |
| @wraps(f) | |
| def wrapper(*args, **kwds): | |
| print('decorated fun wraps') | |
| return f(*args, **kwds) | |
| return wrapper | |
| def decorator_update_wrapper(f): | |
| def wrapper(*args, **kwds): | |
| print('decorated func wraps and update') | |
| return f(*args, **kwds) | |
| return update_wrapper(wrapper, wrapped=f) | |
| @decorator_nowraps | |
| def example1(): | |
| """Docstr ex1""" | |
| print('example1') | |
| @decorator_wraps | |
| def example2(): | |
| """Docstr ex2""" | |
| print('ex2') | |
| @decorator_update_wrapper | |
| def example3(): | |
| """Docstr ex3""" | |
| print('ex3') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment