Skip to content

Commit 4baaa25

Browse files
committed
Added Resources to README and non-@-syntax version of callable object example.
1 parent 52cb577 commit 4baaa25

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

README.markdown

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
# pydecorators
1+
# Decorating Python: Python's Decorators Explained
22

3-
## Beginner to intermediate level presentation on Python decorators.
3+
## Resources
4+
5+
* Graham Dumpleton: [Graham Dumpleton on Decorators](http://blog.dscpl.com.au/search/label/decorators)
6+
* Real Python: [Primer on Python Decorators](https://realpython.com/blog/python/primer-on-python-decorators/)
7+
* Jeff Knupp: [Decorators Explained](http://www.jeffknupp.com/blog/2013/11/29/improve-your-python-decorators-explained/)
8+
* The Code Ship: [A guide to Python's function decorators](http://thecodeship.com/patterns/guide-to-python-function-decorators/)

pydecorators.ipynb

+53-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"metadata": {
3-
"celltoolbar": "Raw Cell Format",
43
"name": "",
5-
"signature": "sha256:b7fafba0fc82a59cb7cf338ba66c116c70894fd255717b655cf92f97673c9043"
4+
"signature": "sha256:9508ad401da191ade544e7d349ad8f99b7ecf0fdcfb71d8696001b5a26a47e70"
65
},
76
"nbformat": 3,
87
"nbformat_minor": 0,
@@ -79,7 +78,7 @@
7978
"\n",
8079
"func()\n",
8180
"\n",
82-
"type(func)"
81+
"print(type(func))"
8382
],
8483
"language": "python",
8584
"metadata": {},
@@ -90,19 +89,12 @@
9089
"text": [
9190
"Before func() is called.\n",
9291
"Behavior from func().\n",
93-
"After func() is called.\n"
94-
]
95-
},
96-
{
97-
"metadata": {},
98-
"output_type": "pyout",
99-
"prompt_number": 2,
100-
"text": [
101-
"function"
92+
"After func() is called.\n",
93+
"<class 'function'>\n"
10294
]
10395
}
10496
],
105-
"prompt_number": 2
97+
"prompt_number": 3
10698
},
10799
{
108100
"cell_type": "markdown",
@@ -144,7 +136,7 @@
144136
]
145137
}
146138
],
147-
"prompt_number": 9
139+
"prompt_number": 1
148140
},
149141
{
150142
"cell_type": "markdown",
@@ -168,11 +160,14 @@
168160
" self.f()\n",
169161
" print(\"After {function_name}() is called.\".format(function_name=self.f.__name__))\n",
170162
" \n",
171-
"@DecoratorCls\n",
172163
"def func():\n",
173164
" print(\"Behavior from func().\")\n",
174165
" \n",
175-
"func()"
166+
"func = DecoratorCls(func)\n",
167+
"\n",
168+
"func()\n",
169+
"\n",
170+
"print(type(func))"
176171
],
177172
"language": "python",
178173
"metadata": {},
@@ -183,26 +178,63 @@
183178
"text": [
184179
"Before func() is called.\n",
185180
"Behavior from func().\n",
186-
"After func() is called.\n"
181+
"After func() is called.\n",
182+
"<class '__main__.DecoratorCls'>\n"
187183
]
188184
}
189185
],
190-
"prompt_number": 4
186+
"prompt_number": 2
191187
},
192188
{
193189
"cell_type": "markdown",
194190
"metadata": {},
195191
"source": [
196-
"## So, Python Decorators are callable objects that modify the behavior of other callable objects."
192+
"#### Again, this is equivalent to the code above."
197193
]
198194
},
199195
{
200196
"cell_type": "code",
201197
"collapsed": false,
202-
"input": [],
198+
"input": [
199+
"class DecoratorCls(object):\n",
200+
" def __init__(self, f):\n",
201+
" self.f = f\n",
202+
" \n",
203+
" def __call__(self):\n",
204+
" print(\"Before {function_name}() is called.\".format(function_name=self.f.__name__))\n",
205+
" self.f()\n",
206+
" print(\"After {function_name}() is called.\".format(function_name=self.f.__name__))\n",
207+
" \n",
208+
"@DecoratorCls\n",
209+
"def func():\n",
210+
" print(\"Behavior from func().\")\n",
211+
" \n",
212+
"func()\n",
213+
"\n",
214+
"print(type(func))"
215+
],
203216
"language": "python",
204217
"metadata": {},
205-
"outputs": []
218+
"outputs": [
219+
{
220+
"output_type": "stream",
221+
"stream": "stdout",
222+
"text": [
223+
"Before func() is called.\n",
224+
"Behavior from func().\n",
225+
"After func() is called.\n",
226+
"<class '__main__.DecoratorCls'>\n"
227+
]
228+
}
229+
],
230+
"prompt_number": 4
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"## So, Python Decorators are callable objects that modify the behavior of other callable objects."
237+
]
206238
}
207239
],
208240
"metadata": {}

0 commit comments

Comments
 (0)