Merge lp:~greatred/python-snippets/core-snippets into lp:~jonobacon/python-snippets/trunk

Proposed by John Beisley
Status: Needs review
Proposed branch: lp:~greatred/python-snippets/core-snippets
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 112 lines (+108/-0)
1 file modified
pythoncore/func_decorator.py (+108/-0)
To merge this branch: bzr merge lp:~greatred/python-snippets/core-snippets
Reviewer Review Type Date Requested Status
Akkana Peck (community) Approve
Jono Bacon Pending
Review via email: mp+24457@code.launchpad.net

Description of the change

Added snippet demonstrating three examples of function decorators in Python.

To post a comment you must log in.
Revision history for this message
Akkana Peck (akkzilla) wrote :

Looks good! Thanks for the contribution. I've just been asked to take over python-snippets, and this will be my first check-in as soon as I figure out how to get bzr to retain your authorship of the snippet.

review: Approve
Revision history for this message
Akkana Peck (akkzilla) wrote :

I've committed your snippet, but apparently I don't have the necessary permission to mark this closed because it was submitted against jono's branch. Submitter, can you mark this closed? (Unless you see any problems with the commit, of course.)

Revision history for this message
John Beisley (greatred) wrote :

I'd completely forgotten about this! I don't see a way to close this issue without deleting it - so feel free to do so if that works for you. I tried to reassign you as the reviewer rather than Jono, but it gives an error saying that you've already reviewed it.

I think deleting this merge request might be the way forwards.

Unmerged revisions

100. By John Beisley <huin@pippin>

Added examples of function decorators.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'pythoncore/func_decorator.py'
2--- pythoncore/func_decorator.py 1970-01-01 00:00:00 +0000
3+++ pythoncore/func_decorator.py 2010-04-29 22:03:30 +0000
4@@ -0,0 +1,108 @@
5+# [SNIPPET_NAME: Function decorators]
6+# [SNIPPET_CATEGORIES: Python Core]
7+# [SNIPPET_DESCRIPTION: Demonstrates simple uses of function decorators]
8+# [SNIPPET_AUTHOR: John Beisley <greatred@gmail.com>]
9+# [SNIPPET_DOCS: http://www.python.org/dev/peps/pep-0318/]
10+# [SNIPPET_LICENSE: BSD]
11+
12+
13+print """
14+##############################################################################
15+# Example 1: A decorator that wraps a function
16+##############################################################################
17+"""
18+
19+def debug_decorator(fn):
20+ """
21+ A decorator that prints the parameters passed to it, and the return value
22+ from the function.
23+ """
24+ # This decorator works by creating a "wrapper" function
25+ def wrapper(*args, **kw):
26+ print "Function call: %s called with *args=%r **kw=%r" % (
27+ fn.__name__, args, kw,
28+ )
29+ # Call the wrapped function with the same arguments, so as to act as a
30+ # transparent wrapper.
31+ retval = fn(*args, **kw)
32+ print "Function call: %s returned %r" % (
33+ fn.__name__, retval,
34+ )
35+ # Return the original value returned, so as to act as a transparent
36+ # wrapper.
37+ return retval
38+
39+ # Return the wrapper function which will take the place of the original fn
40+ return wrapper
41+
42+# Decorators can be used like this:
43+
44+def foo_1(a, b):
45+ print "in foo_1"
46+ return a + b
47+
48+foo_1 = debug_decorator(foo_1)
49+
50+# Showing the decorator at work:
51+print foo_1(1, b=2)
52+
53+# In Python 2.4 and later, the following is equivalent to the above:
54+@debug_decorator
55+def foo_2(a, b):
56+ print "in foo_2"
57+ return a + b
58+
59+# Showing the decorator at work again, same result:
60+print foo_2(1, b=2)
61+
62+
63+
64+print """
65+##############################################################################
66+# Example 2: A decorator that registers a function, but does not alter it
67+##############################################################################
68+"""
69+
70+
71+# A place to register functions:
72+functions = {}
73+
74+def register(fn):
75+ functions[fn.__name__] = fn
76+ # The original function is returned
77+ return fn
78+
79+@register
80+def add(a, b):
81+ return a + b
82+
83+@register
84+def multiply(a, b):
85+ return a * b
86+
87+print functions["add"](4, 6)
88+print functions["multiply"](4, 6)
89+
90+
91+print """
92+##############################################################################
93+# Example 3: Decorating with arguments.
94+##############################################################################
95+"""
96+
97+def say(msg):
98+ def decorator(fn):
99+ def wrapper(*args, **kw):
100+ print "Before %s" % msg
101+ retval = fn(*args, **kw)
102+ print "After %s" % msg
103+ return retval
104+ return wrapper
105+ return decorator
106+
107+@say("Hello!")
108+def some_func():
109+ print "In some_func()"
110+
111+
112+some_func()

Subscribers

People subscribed via source and target branches