Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6120a02a
Commit
6120a02a
authored
Jun 09, 2006
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update functools section
parent
6d572ff1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
10 deletions
+36
-10
Doc/whatsnew/whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+36
-10
No files found.
Doc/whatsnew/whatsnew25.tex
View file @
6120a02a
...
...
@@ -126,19 +126,16 @@ Wouters.}
\section
{
PEP 309: Partial Function Application
\label
{
pep-309
}}
The
\module
{
functools
}
module is intended to contain tools for
functional-style programming. Currently it only contains a
\class
{
partial()
}
function, but new functions will probably be added
in future versions of Python.
functional-style programming.
For programs written in a functional style, it can be useful to
One useful tool in this module is the
\function
{
partial()
}
function.
For programs written in a functional style, you'll sometimes want to
construct variants of existing functions that have some of the
parameters filled in. Consider a Python function
\code
{
f(a, b, c)
}
;
you could create a new function
\code
{
g(b, c)
}
that was equivalent to
\code
{
f(1, b, c)
}
. This is called ``partial function application'',
and is provided by the
\class
{
partial
}
class in the new
\module
{
functools
}
module.
\code
{
f(1, b, c)
}
. This is called ``partial function application''.
The constructor for
\class
{
partial
}
takes the arguments
\function
{
partial
}
takes the arguments
\code
{
(
\var
{
function
}
,
\var
{
arg1
}
,
\var
{
arg2
}
, ...
\var
{
kwarg1
}
=
\var
{
value1
}
,
\var
{
kwarg2
}
=
\var
{
value2
}
)
}
. The resulting
object is callable, so you can just call it to invoke
\var
{
function
}
...
...
@@ -175,11 +172,40 @@ class Application:
\end{verbatim}
Another function in the
\module
{
functools
}
module is the
\function
{
update
_
wrapper(
\var
{
wrapper,
\var
{
wrapped
}
)
}
function that
helps you write well-behaved decorators.
\function
{
update
_
wrapper()
}
copies the name, module, and docstring attribute to a wrapper function
so that tracebacks inside the wrapped function are easier to
understand. For example, you might write:
\begin{verbatim}
def my
_
decorator(f):
def wrapper(*args, **kwds):
print 'Calling decorated function'
return f(*args, **kwds)
functools.update
_
wrapper(wrapper, f)
return wrapper
\end{verbatim}
\function
{
wraps()
}
is a decorator that can be used inside your own
decorators to copy the wrapped function's information. An alternate
version of the previous example would be:
\begin{verbatim}
def my
_
decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwds):
print 'Calling decorated function'
return f(*args, **kwds)
return wrapper
\end{verbatim}
\begin{seealso}
\seepep
{
309
}{
Partial Function Application
}{
PEP proposed and written by
Peter Harris; implemented by Hye-Shik Chang
, with adaptations by
Raymond Hettinger.
}
Peter Harris; implemented by Hye-Shik Chang
and Nick Coghlan, with
adaptations by
Raymond Hettinger.
}
\end{seealso}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment