Skip to content

Commit 642ba4a

Browse files
gh-137668: Document that ord() supports also bytes and bytearray
1 parent 027cacb commit 642ba4a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Doc/library/functions.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,13 +1562,18 @@ 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
1567+
The argument must be a one-character string or a :class:`bytes` or
1568+
:class:`bytearray` object of length 1.
1569+
If it is a one-character string, return an integer
15681570
representing the Unicode code point 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 an integer value of its element.
1576+
15721577

15731578
.. function:: pow(base, exp, mod=None)
15741579

Python/bltinmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,15 +2118,18 @@ 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
21242124
Return the Unicode code point for a one-character string.
2125+
2126+
Return the value of the first byte for a bytes or bytearray object of
2127+
length 1.
21252128
[clinic start generated code]*/
21262129

21272130
static PyObject *
21282131
builtin_ord(PyObject *module, PyObject *c)
2129-
/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
2132+
/*[clinic end generated code: output=4fa5e87a323bae71 input=99e491d094680598]*/
21302133
{
21312134
long ord;
21322135
Py_ssize_t size;

Python/clinic/bltinmodule.c.h

Lines changed: 6 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)