Skip to content

Commit 207d1b0

Browse files
[3.12] gh-109218: Improve documentation for the complex() constructor (GH-119687) (ПР-119805)
* Remove the equivalence with real+imag*1j which can be incorrect in corner cases (non-finite numbers, the sign of zeroes). * Separately document the three roles of the constructor: parsing a string, converting a number, and constructing a complex from components. * Document positional-only parameters of complex(), float(), int() and bool() as positional-only. * Add examples for complex() and int(). * Specify the grammar of the string for complex(). * Improve the grammar of the string for float(). * Describe more explicitly the behavior when real and/or imag arguments are complex numbers. (This will be deprecated in future.) (cherry picked from commit ec1ba26)
1 parent 2e31e56 commit 207d1b0

File tree

4 files changed

+135
-63
lines changed

4 files changed

+135
-63
lines changed

Doc/library/cmath.rst

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ Conversions to and from polar coordinates
4343

4444
A Python complex number ``z`` is stored internally using *rectangular*
4545
or *Cartesian* coordinates. It is completely determined by its *real
46-
part* ``z.real`` and its *imaginary part* ``z.imag``. In other
47-
words::
48-
49-
z == z.real + z.imag*1j
46+
part* ``z.real`` and its *imaginary part* ``z.imag``.
5047

5148
*Polar coordinates* give an alternative way to represent a complex
5249
number. In polar coordinates, a complex number *z* is defined by the
@@ -90,7 +87,7 @@ rectangular coordinates to polar coordinates and back.
9087
.. function:: rect(r, phi)
9188

9289
Return the complex number *x* with polar coordinates *r* and *phi*.
93-
Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``.
90+
Equivalent to ``complex(r * math.cos(phi), r * math.sin(phi))``.
9491

9592

9693
Power and logarithmic functions

Doc/library/functions.rst

+121-52
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ are always available. They are listed here in alphabetical order.
141141
See also :func:`format` for more information.
142142

143143

144-
.. class:: bool(x=False)
144+
.. class:: bool(object=False, /)
145145

146-
Return a Boolean value, i.e. one of ``True`` or ``False``. *x* is converted
147-
using the standard :ref:`truth testing procedure <truth>`. If *x* is false
146+
Return a Boolean value, i.e. one of ``True`` or ``False``. The argument
147+
is converted using the standard :ref:`truth testing procedure <truth>`.
148+
If the argument is false
148149
or omitted, this returns ``False``; otherwise, it returns ``True``. The
149150
:class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`).
150151
It cannot be subclassed further. Its only instances are ``False`` and
@@ -153,7 +154,7 @@ are always available. They are listed here in alphabetical order.
153154
.. index:: pair: Boolean; type
154155

155156
.. versionchanged:: 3.7
156-
*x* is now a positional-only parameter.
157+
The parameter is now positional-only.
157158

158159
.. function:: breakpoint(*args, **kws)
159160

@@ -371,29 +372,73 @@ are always available. They are listed here in alphabetical order.
371372
support for top-level ``await``, ``async for``, and ``async with``.
372373

373374

374-
.. class:: complex(real=0, imag=0)
375-
complex(string)
375+
.. class:: complex(number=0, /)
376+
complex(string, /)
377+
complex(real=0, imag=0)
378+
379+
Convert a single string or number to a complex number, or create a
380+
complex number from real and imaginary parts.
381+
382+
Examples:
383+
384+
.. doctest::
385+
386+
>>> complex('+1.23')
387+
(1.23+0j)
388+
>>> complex('-4.5j')
389+
-4.5j
390+
>>> complex('-1.23+4.5j')
391+
(-1.23+4.5j)
392+
>>> complex('\t( -1.23+4.5J )\n')
393+
(-1.23+4.5j)
394+
>>> complex('-Infinity+NaNj')
395+
(-inf+nanj)
396+
>>> complex(1.23)
397+
(1.23+0j)
398+
>>> complex(imag=-4.5)
399+
-4.5j
400+
>>> complex(-1.23, 4.5)
401+
(-1.23+4.5j)
402+
403+
If the argument is a string, it must contain either a real part (in the
404+
same format as for :func:`float`) or an imaginary part (in the same
405+
format but with a ``'j'`` or ``'J'`` suffix), or both real and imaginary
406+
parts (the sign of the imaginary part is mandatory in this case).
407+
The string can optionally be surrounded by whitespaces and the round
408+
parentheses ``'('`` and ``')'``, which are ignored.
409+
The string must not contain whitespace between ``'+'``, ``'-'``, the
410+
``'j'`` or ``'J'`` suffix, and the decimal number.
411+
For example, ``complex('1+2j')`` is fine, but ``complex('1 + 2j')`` raises
412+
:exc:`ValueError`.
413+
More precisely, the input must conform to the :token:`~float:complexvalue`
414+
production rule in the following grammar, after parentheses and leading and
415+
trailing whitespace characters are removed:
376416

377-
Return a complex number with the value *real* + *imag*\*1j or convert a string
378-
or number to a complex number. If the first parameter is a string, it will
379-
be interpreted as a complex number and the function must be called without a
380-
second parameter. The second parameter can never be a string. Each argument
381-
may be any numeric type (including complex). If *imag* is omitted, it
382-
defaults to zero and the constructor serves as a numeric conversion like
383-
:class:`int` and :class:`float`. If both arguments are omitted, returns
384-
``0j``.
417+
.. productionlist:: float
418+
complexvalue: `floatvalue` |
419+
: `floatvalue` ("j" | "J") |
420+
: `floatvalue` `sign` `absfloatvalue` ("j" | "J")
385421

422+
If the argument is a number, the constructor serves as a numeric
423+
conversion like :class:`int` and :class:`float`.
386424
For a general Python object ``x``, ``complex(x)`` delegates to
387-
``x.__complex__()``. If :meth:`~object.__complex__` is not defined then it falls back
388-
to :meth:`~object.__float__`. If :meth:`!__float__` is not defined then it falls back
425+
``x.__complex__()``.
426+
If :meth:`~object.__complex__` is not defined then it falls back
427+
to :meth:`~object.__float__`.
428+
If :meth:`!__float__` is not defined then it falls back
389429
to :meth:`~object.__index__`.
390430

391-
.. note::
431+
If two arguments are provided or keyword arguments are used, each argument
432+
may be any numeric type (including complex).
433+
If both arguments are real numbers, return a complex number with the real
434+
component *real* and the imaginary component *imag*.
435+
If both arguments are complex numbers, return a complex number with the real
436+
component ``real.real-imag.imag`` and the imaginary component
437+
``real.imag+imag.real``.
438+
If one of arguments is a real number, only its real component is used in
439+
the above expressions.
392440

393-
When converting from a string, the string must not contain whitespace
394-
around the central ``+`` or ``-`` operator. For example,
395-
``complex('1+2j')`` is fine, but ``complex('1 + 2j')`` raises
396-
:exc:`ValueError`.
441+
If all arguments are omitted, returns ``0j``.
397442

398443
The complex type is described in :ref:`typesnumeric`.
399444

@@ -662,21 +707,38 @@ are always available. They are listed here in alphabetical order.
662707
elements of *iterable* for which *function* is false.
663708

664709

665-
.. class:: float(x=0.0)
710+
.. class:: float(number=0.0, /)
711+
float(string, /)
666712

667713
.. index::
668714
single: NaN
669715
single: Infinity
670716

671-
Return a floating point number constructed from a number or string *x*.
717+
Return a floating point number constructed from a number or a string.
718+
719+
Examples:
720+
721+
.. doctest::
722+
723+
>>> float('+1.23')
724+
1.23
725+
>>> float(' -12345\n')
726+
-12345.0
727+
>>> float('1e-003')
728+
0.001
729+
>>> float('+1E6')
730+
1000000.0
731+
>>> float('-Infinity')
732+
-inf
672733

673734
If the argument is a string, it should contain a decimal number, optionally
674735
preceded by a sign, and optionally embedded in whitespace. The optional
675736
sign may be ``'+'`` or ``'-'``; a ``'+'`` sign has no effect on the value
676737
produced. The argument may also be a string representing a NaN
677-
(not-a-number), or positive or negative infinity. More precisely, the
678-
input must conform to the ``floatvalue`` production rule in the following
679-
grammar, after leading and trailing whitespace characters are removed:
738+
(not-a-number), or positive or negative infinity.
739+
More precisely, the input must conform to the :token:`~float:floatvalue`
740+
production rule in the following grammar, after leading and trailing
741+
whitespace characters are removed:
680742

681743
.. productionlist:: float
682744
sign: "+" | "-"
@@ -685,9 +747,10 @@ are always available. They are listed here in alphabetical order.
685747
digit: <a Unicode decimal digit, i.e. characters in Unicode general category Nd>
686748
digitpart: `digit` (["_"] `digit`)*
687749
number: [`digitpart`] "." `digitpart` | `digitpart` ["."]
688-
exponent: ("e" | "E") ["+" | "-"] `digitpart`
689-
floatnumber: number [`exponent`]
690-
floatvalue: [`sign`] (`floatnumber` | `infinity` | `nan`)
750+
exponent: ("e" | "E") [`sign`] `digitpart`
751+
floatnumber: `number` [`exponent`]
752+
absfloatvalue: `floatnumber` | `infinity` | `nan`
753+
floatvalue: [`sign`] `absfloatvalue`
691754

692755
Case is not significant, so, for example, "inf", "Inf", "INFINITY", and
693756
"iNfINity" are all acceptable spellings for positive infinity.
@@ -703,26 +766,13 @@ are always available. They are listed here in alphabetical order.
703766

704767
If no argument is given, ``0.0`` is returned.
705768

706-
Examples::
707-
708-
>>> float('+1.23')
709-
1.23
710-
>>> float(' -12345\n')
711-
-12345.0
712-
>>> float('1e-003')
713-
0.001
714-
>>> float('+1E6')
715-
1000000.0
716-
>>> float('-Infinity')
717-
-inf
718-
719769
The float type is described in :ref:`typesnumeric`.
720770

721771
.. versionchanged:: 3.6
722772
Grouping digits with underscores as in code literals is allowed.
723773

724774
.. versionchanged:: 3.7
725-
*x* is now a positional-only parameter.
775+
The parameter is now positional-only.
726776

727777
.. versionchanged:: 3.8
728778
Falls back to :meth:`~object.__index__` if :meth:`~object.__float__` is not defined.
@@ -906,17 +956,36 @@ are always available. They are listed here in alphabetical order.
906956
with the result after successfully reading input.
907957

908958

909-
.. class:: int(x=0)
910-
int(x, base=10)
959+
.. class:: int(number=0, /)
960+
int(string, /, base=10)
961+
962+
Return an integer object constructed from a number or a string, or return
963+
``0`` if no arguments are given.
964+
965+
Examples:
966+
967+
.. doctest::
968+
969+
>>> int(123.45)
970+
123
971+
>>> int('123')
972+
123
973+
>>> int(' -12_345\n')
974+
-12345
975+
>>> int('FACE', 16)
976+
64206
977+
>>> int('0xface', 0)
978+
64206
979+
>>> int('01110011', base=2)
980+
115
911981

912-
Return an integer object constructed from a number or string *x*, or return
913-
``0`` if no arguments are given. If *x* defines :meth:`~object.__int__`,
914-
``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`~object.__index__`,
915-
it returns ``x.__index__()``. If *x* defines :meth:`~object.__trunc__`,
982+
If the argument defines :meth:`~object.__int__`,
983+
``int(x)`` returns ``x.__int__()``. If the argument defines :meth:`~object.__index__`,
984+
it returns ``x.__index__()``. If the argument defines :meth:`~object.__trunc__`,
916985
it returns ``x.__trunc__()``.
917986
For floating point numbers, this truncates towards zero.
918987

919-
If *x* is not a number or if *base* is given, then *x* must be a string,
988+
If the argument is not a number or if *base* is given, then it must be a string,
920989
:class:`bytes`, or :class:`bytearray` instance representing an integer
921990
in radix *base*. Optionally, the string can be preceded by ``+`` or ``-``
922991
(with no space in between), have leading zeros, be surrounded by whitespace,
@@ -946,7 +1015,7 @@ are always available. They are listed here in alphabetical order.
9461015
Grouping digits with underscores as in code literals is allowed.
9471016

9481017
.. versionchanged:: 3.7
949-
*x* is now a positional-only parameter.
1018+
The first parameter is now positional-only.
9501019

9511020
.. versionchanged:: 3.8
9521021
Falls back to :meth:`~object.__index__` if :meth:`~object.__int__` is not defined.
@@ -957,7 +1026,7 @@ are always available. They are listed here in alphabetical order.
9571026
.. versionchanged:: 3.11
9581027
:class:`int` string inputs and string representations can be limited to
9591028
help avoid denial of service attacks. A :exc:`ValueError` is raised when
960-
the limit is exceeded while converting a string *x* to an :class:`int` or
1029+
the limit is exceeded while converting a string to an :class:`int` or
9611030
when converting an :class:`int` into a string would exceed the limit.
9621031
See the :ref:`integer string conversion length limitation
9631032
<int_max_str_digits>` documentation.

Objects/clinic/complexobject.c.h

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/complexobject.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,17 @@ complex.__new__ as complex_new
885885
real as r: object(c_default="NULL") = 0
886886
imag as i: object(c_default="NULL") = 0
887887
888-
Create a complex number from a real part and an optional imaginary part.
888+
Create a complex number from a string or numbers.
889889
890-
This is equivalent to (real + imag*1j) where imag defaults to 0.
890+
If a string is given, parse it as a complex number.
891+
If a single number is given, convert it to a complex number.
892+
If the 'real' or 'imag' arguments are given, create a complex number
893+
with the specified real and imaginary components.
891894
[clinic start generated code]*/
892895

893896
static PyObject *
894897
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
895-
/*[clinic end generated code: output=b6c7dd577b537dc1 input=f4c667f2596d4fd1]*/
898+
/*[clinic end generated code: output=b6c7dd577b537dc1 input=ff4268dc540958a4]*/
896899
{
897900
PyObject *tmp;
898901
PyNumberMethods *nbr, *nbi = NULL;

0 commit comments

Comments
 (0)