-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-109218: Improve documentation for the complex() constructor #119687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e67380d
94deb98
7182597
63ebe03
28bae65
944dc6c
97a9e00
d77833c
9664b6a
c23c659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -379,6 +379,27 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Convert a single string or number to a complex number, or create a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complex number from real and imaginary parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('+1.23') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1.23+0j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-4.5j') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-4.5j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-1.23+4.5j') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('\t( -1.23+4.5J )\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-Infinity+NaNj') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-inf+nanj) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(1.23) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1.23+0j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(imag=-4.5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-4.5j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(-1.23, 4.5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+386
to
+401
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest we reorder some of these so that the stranger ones come below the more common ones:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this mixes examples for three fundamentally different roles: string parsing, numerical conversion and construction from components. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wasn't immediately obvious to me that that was the reason behind your ordering, and I doubt it would be obvious to beginners either, which is why I suggested a different order of "most useful to least useful". But again, I don't have a strong opinion; your PR is certainly fine as-is :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also expect the most useful to the least useful examples even though it mixes the parsers. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument is a string, it should contain a decimal number, optionally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
preceded by a sign, and optionally followed by the ``j`` or ``J`` suffix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
or a pair of decimal numbers, optionally preceded by a sign, separated by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -412,27 +433,6 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If all arguments are omitted, returns ``0j``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('+1.23') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1.23+0j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-4.5j') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-4.5j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-1.23+4.5j') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('\t( -1.23+4.5J )\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex('-Infinity+NaNj') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-inf+nanj) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(1.23) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1.23+0j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(imag=-4.5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-4.5j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> complex(-1.23, 4.5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(-1.23+4.5j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The complex type is described in :ref:`typesnumeric`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -729,6 +729,21 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return a floating point number constructed from a number or a string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('+1.23') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1.23 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float(' -12345\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-12345.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('1e-003') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0.001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('+1E6') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1000000.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('-Infinity') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-inf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument is a string, it should contain a decimal number, optionally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
preceded by a sign, and optionally embedded in whitespace. The optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sign may be ``'+'`` or ``'-'``; a ``'+'`` sign has no effect on the value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -762,19 +777,6 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If no argument is given, ``0.0`` is returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('+1.23') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1.23 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float(' -12345\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-12345.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('1e-003') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0.001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('+1E6') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1000000.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> float('-Infinity') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-inf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The float type is described in :ref:`typesnumeric`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -971,6 +973,21 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return an integer object constructed from a number or a string, or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``0`` if no arguments are given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('123') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
123 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int(' -12_345\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-12345 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('FACE', 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
64206 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('0xface', 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
64206 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('01110011', base=2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
115 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument defines :meth:`~object.__int__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``int(x)`` returns ``x.__int__()``. If the argument defines :meth:`~object.__index__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it returns ``x.__index__()``. If the argument defines :meth:`~object.__trunc__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may look a bit nicer, but I don't know whether you want a smaller heading here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking how it is used in
Doc/library/asyncio-api-index.rst
, I do not think that it is appropriate here.