Skip to content

Commit cf24180

Browse files
gh-119127: functools: Improve docs for partial and Placeholder (#124575)
1 parent 09aebb1 commit cf24180

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Diff for: Doc/library/functools.rst

+17-15
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ The :mod:`functools` module defines the following functions:
354354
newfunc.keywords = keywords
355355
return newfunc
356356

357-
The :func:`partial` function is used for partial function application which "freezes"
357+
The :func:`!partial` function is used for partial function application which "freezes"
358358
some portion of a function's arguments and/or keywords resulting in a new object
359359
with a simplified signature. For example, :func:`partial` can be used to create
360360
a callable that behaves like the :func:`int` function where the *base* argument
@@ -368,25 +368,27 @@ The :mod:`functools` module defines the following functions:
368368
18
369369

370370
If :data:`Placeholder` sentinels are present in *args*, they will be filled first
371-
when :func:`partial` is called. This allows custom selection of positional arguments
372-
to be pre-filled when constructing a :ref:`partial object <partial-objects>`.
371+
when :func:`!partial` is called. This makes it possible to pre-fill any positional
372+
argument with a call to :func:`!partial`; without :data:`!Placeholder`, only the
373+
first positional argument can be pre-filled.
373374

374-
If :data:`!Placeholder` sentinels are present, all of them must be filled at call time:
375+
If any :data:`!Placeholder` sentinels are present, all must be filled at call time:
375376

376377
.. doctest::
377378

378379
>>> say_to_world = partial(print, Placeholder, Placeholder, "world!")
379380
>>> say_to_world('Hello', 'dear')
380381
Hello dear world!
381382

382-
Calling ``say_to_world('Hello')`` would raise a :exc:`TypeError`, because
383-
only one positional argument is provided, while there are two placeholders
384-
in :ref:`partial object <partial-objects>`.
383+
Calling ``say_to_world('Hello')`` raises a :exc:`TypeError`, because
384+
only one positional argument is provided, but there are two placeholders
385+
that must be filled in.
385386

386-
Successive :func:`partial` applications fill :data:`!Placeholder` sentinels
387-
of the input :func:`partial` objects with new positional arguments.
388-
A place for positional argument can be retained by inserting new
389-
:data:`!Placeholder` sentinel to the place held by previous :data:`!Placeholder`:
387+
If :func:`!partial` is applied to an existing :func:`!partial` object,
388+
:data:`!Placeholder` sentinels of the input object are filled in with
389+
new positional arguments.
390+
A placeholder can be retained by inserting a new
391+
:data:`!Placeholder` sentinel to the place held by a previous :data:`!Placeholder`:
390392

391393
.. doctest::
392394

@@ -402,8 +404,8 @@ The :mod:`functools` module defines the following functions:
402404
>>> remove_first_dear(message)
403405
'Hello, dear world!'
404406

405-
Note, :data:`!Placeholder` has no special treatment when used for keyword
406-
argument of :data:`!Placeholder`.
407+
:data:`!Placeholder` has no special treatment when used in a keyword
408+
argument to :func:`!partial`.
407409

408410
.. versionchanged:: 3.14
409411
Added support for :data:`Placeholder` in positional arguments.
@@ -791,7 +793,7 @@ have three read-only attributes:
791793
The keyword arguments that will be supplied when the :class:`partial` object is
792794
called.
793795

794-
:class:`partial` objects are like :class:`function` objects in that they are
796+
:class:`partial` objects are like :ref:`function objects <user-defined-funcs>` in that they are
795797
callable, weak referenceable, and can have attributes. There are some important
796-
differences. For instance, the :attr:`~definition.__name__` and :attr:`__doc__` attributes
798+
differences. For instance, the :attr:`~definition.__name__` and :attr:`~definition.__doc__` attributes
797799
are not created automatically.

0 commit comments

Comments
 (0)