Skip to content

Commit 35759fe

Browse files
gh-137668: Document that ord() supports also bytes and bytearray (GH-137669)
1 parent 639df39 commit 35759fe

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
@@ -1562,13 +1562,19 @@ are always available. They are listed here in alphabetical order.
15621562
.. versionchanged:: 3.11
15631563
The ``'U'`` mode has been removed.
15641564

1565-
.. function:: ord(c)
1565+
.. function:: ord(character, /)
15661566

1567-
Given a string representing one Unicode character, return an integer
1568-
representing the Unicode code point of that character. For example,
1567+
Return the ordinal value of a character.
1568+
1569+
If the argument is a one-character string, return the Unicode code point
1570+
of that character. For example,
15691571
``ord('a')`` returns the integer ``97`` and ``ord('€')`` (Euro sign)
15701572
returns ``8364``. This is the inverse of :func:`chr`.
15711573

1574+
If the argument is a :class:`bytes` or :class:`bytearray` object of
1575+
length 1, return its single byte value.
1576+
For example, ``ord(b'a')`` returns the integer ``97``.
1577+
15721578

15731579
.. function:: pow(base, exp, mod=None)
15741580

Python/bltinmodule.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,15 +2118,21 @@ builtin_oct(PyObject *module, PyObject *number)
21182118
/*[clinic input]
21192119
ord as builtin_ord
21202120
2121-
c: object
2121+
character as c: object
21222122
/
21232123
2124-
Return the Unicode code point for a one-character string.
2124+
Return the ordinal value of a character.
2125+
2126+
If the argument is a one-character string, return the Unicode code
2127+
point of that character.
2128+
2129+
If the argument is a bytes or bytearray object of length 1, return its
2130+
single byte value.
21252131
[clinic start generated code]*/
21262132

21272133
static PyObject *
21282134
builtin_ord(PyObject *module, PyObject *c)
2129-
/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
2135+
/*[clinic end generated code: output=4fa5e87a323bae71 input=98d38480432e1177]*/
21302136
{
21312137
long ord;
21322138
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)