Skip to content

Commit 87f5255

Browse files
bpo-21314: Add a FAQ entry about positional only parameters (GH-10641)
(cherry picked from commit 1aeeaeb) Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
1 parent ae2378a commit 87f5255

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Doc/faq/programming.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,41 @@ Yes. Usually this is done by nesting :keyword:`lambda` within
767767
Don't try this at home, kids!
768768

769769

770+
.. _faq-positional-only-arguments:
771+
772+
What does the slash(/) in the parameter list of a function mean?
773+
----------------------------------------------------------------
774+
775+
A slash in the argument list of a function denotes that the parameters prior to
776+
it are positional-only. Positional-only parameters are the ones without an
777+
externally-usable name. Upon calling a function that accepts positional-only
778+
parameters, arguments are mapped to parameters based solely on their position.
779+
For example, :func:`pow` is a function that accepts positional-only parameters.
780+
Its documentation looks like this::
781+
782+
>>> help(pow)
783+
Help on built-in function pow in module builtins:
784+
785+
pow(x, y, z=None, /)
786+
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
787+
788+
Some types, such as ints, are able to use a more efficient algorithm when
789+
invoked using the three argument form.
790+
791+
The slash at the end of the parameter list means that all three parameters are
792+
positional-only. Thus, calling :func:`pow` with keyword aguments would lead to
793+
an error::
794+
795+
>>> pow(x=3, y=4)
796+
Traceback (most recent call last):
797+
File "<stdin>", line 1, in <module>
798+
TypeError: pow() takes no keyword arguments
799+
800+
Note that as of this writing this is only documentational and no valid syntax
801+
in Python, although there is :pep:`570`, which proposes a syntax for
802+
position-only parameters in Python.
803+
804+
770805
Numbers and strings
771806
===================
772807

Doc/library/functions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ are always available. They are listed here in alphabetical order.
668668
topic, and a help page is printed on the console. If the argument is any other
669669
kind of object, a help page on the object is generated.
670670

671+
Note that if a slash(/) appears in the parameter list of a function, when
672+
invoking :func:`help`, it means that the parameters prior to the slash are
673+
positional-only. For more info, see
674+
:ref:`the FAQ entry on positional-only parameters <faq-positional-only-arguments>`.
675+
671676
This function is added to the built-in namespace by the :mod:`site` module.
672677

673678
.. versionchanged:: 3.4

Doc/library/inspect.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ function.
559559
Raises :exc:`ValueError` if no signature can be provided, and
560560
:exc:`TypeError` if that type of object is not supported.
561561

562+
A slash(/) in the signature of a function denotes that the parameters prior
563+
to it are positional-only. For more info, see
564+
:ref:`the FAQ entry on positional-only parameters <faq-positional-only-arguments>`.
565+
562566
.. versionadded:: 3.5
563567
``follow_wrapped`` parameter. Pass ``False`` to get a signature of
564568
``callable`` specifically (``callable.__wrapped__`` will not be used to
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A new entry was added to the Core Language Section of the Programming FAQ,
2+
which explaines the usage of slash(/) in the signature of a function. Patch
3+
by Lysandros Nikolaou

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy