Skip to content

Instantly share code, notes, and snippets.

@godber
Created July 24, 2014 00:33
Show Gist options
  • Select an option

  • Save godber/392d5c57950298a24cb0 to your computer and use it in GitHub Desktop.

Select an option

Save godber/392d5c57950298a24cb0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"celltoolbar": "Slideshow",
"name": "",
"signature": "sha256:c6e3f0745d29381e55149244fa2f63c7a5151b305023b8202582e6b7268865a0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Decorators and Properties\n",
"\n",
"### Austin Godber\n",
"#### @godber\n",
"\n",
"July 2014"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# PEP 318 - Python 2.4\n",
"\n",
"* A decorator is a function that returns another function\n",
"* Typically modifies the behavior of passed in function\n",
"* Works on functions or methods\n",
"* http://legacy.python.org/dev/peps/pep-0318/\n",
"* https://wiki.python.org/moin/PythonDecorators"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Background\n",
"\n",
"* Functions are objects.\n",
"* Functions can be passed in as arguments and returned."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Identity Decorator (from @BrianHoldefehr)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def identity_decorator(func):\n",
" def wrapper():\n",
" func()\n",
" return wrapper\n",
"\n",
"def a_function():\n",
" print \"I'm a normal function.\"\n",
"\n",
"decorated_function = identity_decorator(a_function)\n",
"decorated_function()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I'm a normal function.\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Using the @ notation"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@identity_decorator\n",
"def another_decorated_function():\n",
" print \"I too am a normal function\"\n",
" \n",
"another_decorated_function()"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I too am a normal function\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Notation\n",
"\n",
"Using the `@` and placing it **before** the function to be decorated puts it **in-your-face**.\n",
"\n",
"- Guido van Rossum (Fri Aug 6 16:45:51 CEST 2004)\n",
"\n",
"Highlights the function modification instead of burying it at the end as a variable assignment.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Decorator Ordering\n",
"\n",
"```\n",
"@dec2\n",
"@dec1\n",
"def function1():\n",
" pass\n",
"```\n",
"\n",
"equivalent to\n",
"\n",
"```\n",
"def function1():\n",
" pass\n",
"func = dec2(dec1(function1))\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Passing Arguments to Decorators\n",
"\n",
"You can do it."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def increment(val):\n",
" return val + 1\n",
"\n",
"increment(1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"2"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# In Use\n",
"\n",
"* Click `@click.command`\n",
"* Flask `@app.route`"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment