Skip to content

Commit 3a74d52

Browse files
[3.13] pythongh-137668: Document that ord() supports also bytes and bytearray (pythonGH-137669) (pythonGH-137704)
(cherry picked from commit 35759fe)
1 parent 165532c commit 3a74d52

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Doc/library/functions.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,13 +1550,19 @@ are always available. They are listed here in alphabetical order.
15501550
.. versionchanged:: 3.11
15511551
The ``'U'`` mode has been removed.
15521552

1553-
.. function:: ord(c)
1553+
.. function:: ord(character, /)
15541554

1555-
Given a string representing one Unicode character, return an integer
1556-
representing the Unicode code point of that character. For example,
1555+
Return the ordinal value of a character.
1556+
1557+
If the argument is a one-character string, return the Unicode code point
1558+
of that character. For example,
15571559
``ord('a')`` returns the integer ``97`` and ``ord('€')`` (Euro sign)
15581560
returns ``8364``. This is the inverse of :func:`chr`.
15591561

1562+
If the argument is a :class:`bytes` or :class:`bytearray` object of
1563+
length 1, return its single byte value.
1564+
For example, ``ord(b'a')`` returns the integer ``97``.
1565+
15601566

15611567
.. function:: pow(base, exp, mod=None)
15621568

Python/bltinmodule.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,15 +1969,21 @@ builtin_oct(PyObject *module, PyObject *number)
19691969
/*[clinic input]
19701970
ord as builtin_ord
19711971
1972-
c: object
1972+
character as c: object
19731973
/
19741974
1975-
Return the Unicode code point for a one-character string.
1975+
Return the ordinal value of a character.
1976+
1977+
If the argument is a one-character string, return the Unicode code
1978+
point of that character.
1979+
1980+
If the argument is a bytes or bytearray object of length 1, return its
1981+
single byte value.
19761982
[clinic start generated code]*/
19771983

19781984
static PyObject *
19791985
builtin_ord(PyObject *module, PyObject *c)
1980-
/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
1986+
/*[clinic end generated code: output=4fa5e87a323bae71 input=98d38480432e1177]*/
19811987
{
19821988
long ord;
19831989
Py_ssize_t size;

Python/clinic/bltinmodule.c.h

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)