-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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 7 commits
e67380d
94deb98
7182597
63ebe03
28bae65
944dc6c
97a9e00
d77833c
9664b6a
c23c659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -141,10 +141,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
See also :func:`format` for more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: bool(x=False) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: bool(object=False, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return a Boolean value, i.e. one of ``True`` or ``False``. *x* is converted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
using the standard :ref:`truth testing procedure <truth>`. If *x* is false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return a Boolean value, i.e. one of ``True`` or ``False``. The argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is converted using the standard :ref:`truth testing procedure <truth>`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument is false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
or omitted, this returns ``False``; otherwise, it returns ``True``. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It cannot be subclassed further. Its only instances are ``False`` and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -153,7 +154,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. index:: pair: Boolean; type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*x* is now a positional-only parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The parameter is now positional-only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. function:: breakpoint(*args, **kws) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -371,29 +372,73 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
support for top-level ``await``, ``async for``, and ``async with``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: complex(real=0, imag=0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complex(string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: complex(number=0, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complex(string, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complex(real=0, imag=0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Convert a single string or number to a complex number, or create a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complex number from real and imaginary parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
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 commentThe reason will be displayed to describe this comment to others. Learn more. Looking how it is used in |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. 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 must contain either a real part (in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
same format as for :func:`float`) or an imaginary part (in the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format but with a ``'j'`` or ``'J'`` suffix ), or both real and imaginary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
parts (the sign of the imaginary part is mandatory in this case). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The string can optionally be surrounded by whitespaces and the round | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
parenthesis ``'('`` and ``')'``, which are ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The string must not contain whitespace between ``'+'``, ``'-'``, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``'j'`` or ``'J'`` suffix, and the decimal number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Nit: this "j" vs "J" happens in several places. You could just mention (as in the float constructor) that parsing is case-insensitive: >>> complex('(InFiNITY+1j)')
(inf+1j) 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 "e" and "E" are mentioned separately in the float grammar. We can say also that all whitespaces except the leading and trailing are not acceptable, but I think it will be clearer to be a little more verbose here. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For example, ``complex('1+2j')`` is fine, but ``complex('1 + 2j')`` raises | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:exc:`ValueError`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
More precisely, the input must conform to the :token:`~float:complexvalue` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
production rule in the following grammar, after parenthesis and leading and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trailing whitespace characters are removed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return a complex number with the value *real* + *imag*\*1j or convert a string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
or number to a complex number. If the first parameter is a string, it will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
be interpreted as a complex number and the function must be called without a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
second parameter. The second parameter can never be a string. Each argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
may be any numeric type (including complex). If *imag* is omitted, it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
defaults to zero and the constructor serves as a numeric conversion like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:class:`int` and :class:`float`. If both arguments are omitted, returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``0j``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. productionlist:: float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
complexvalue: `floatvalue` | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
: `floatvalue` ("j" | "J") | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
: `floatvalue` `sign` `absfloatvalue` ("j" | "J") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument is a number, the constructor serves as a numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conversion like :class:`int` and :class:`float`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For a general Python object ``x``, ``complex(x)`` delegates to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``x.__complex__()``. If :meth:`~object.__complex__` is not defined then it falls back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
to :meth:`~object.__float__`. If :meth:`!__float__` is not defined then it falls back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``x.__complex__()``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If :meth:`~object.__complex__` is not defined then it falls back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
to :meth:`~object.__float__`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If :meth:`!__float__` is not defined then it falls back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
to :meth:`~object.__index__`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. note:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If two arguments are provided or keyword arguments are used, each argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
may be any numeric type (including complex). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If both arguments are real numbers, return a complex number with the real | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
component *real* and the imaginary component *imag*. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If both arguments are complex numbers, return a complex number with the real | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
component ``real.real-imag.imag`` and the imaginary component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``real.imag+imag.real``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+435
to
+437
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. Presumably this will mention the deprecation if/when #119620 is merged? Or is the plan to remove the mention of allowing complex altogether? I'd be tempted to not even document this (mis)feature, on the basis that people who already know about it already know about it, and people who don't know about it shouldn't start using it. 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. The plan was to leave this until the end of the deprecation period. I would be happy to remove this, but then we will receive another reports about wrong documentation or behavior. The old "Each argument may be any numeric type (including complex)." is already in the current documentation, so it is late to hide this without also changing the behavior. 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. Fair enough. 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. Best we can do is to put the deprecation note as close to this sentence, as possible:) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If one of arguments is a real number, only its real component is used in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the above expressions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When converting from a string, the string must not contain whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
around the central ``+`` or ``-`` operator. For example, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``complex('1+2j')`` is fine, but ``complex('1 + 2j')`` raises | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:exc:`ValueError`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If all arguments are omitted, returns ``0j``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The complex type is described in :ref:`typesnumeric`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -682,21 +727,38 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elements of *iterable* for which *function* is false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: float(x=0.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: float(number=0.0, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
float(string, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. index:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
single: NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
single: Infinity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return a floating point number constructed from a number or string *x*. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
produced. The argument may also be a string representing a NaN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(not-a-number), or positive or negative infinity. More precisely, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
input must conform to the ``floatvalue`` production rule in the following | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
grammar, after leading and trailing whitespace characters are removed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(not-a-number), or positive or negative infinity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
More precisely, the input must conform to the :token:`~float:`floatvalue` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
production rule in the following grammar, after leading and trailing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
whitespace characters are removed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. productionlist:: float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sign: "+" | "-" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -705,9 +767,10 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
digit: <a Unicode decimal digit, i.e. characters in Unicode general category Nd> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
digitpart: `digit` (["_"] `digit`)* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
number: [`digitpart`] "." `digitpart` | `digitpart` ["."] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exponent: ("e" | "E") ["+" | "-"] `digitpart` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floatnumber: number [`exponent`] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floatvalue: [`sign`] (`floatnumber` | `infinity` | `nan`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exponent: ("e" | "E") [`sign`] `digitpart` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floatnumber: `number` [`exponent`] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
absfloatvalue: `floatnumber` | `infinity` | `nan` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floatvalue: [`sign`] `absfloatvalue` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Case is not significant, so, for example, "inf", "Inf", "INFINITY", and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"iNfINity" are all acceptable spellings for positive infinity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -723,26 +786,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Grouping digits with underscores as in code literals is allowed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*x* is now a positional-only parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The parameter is now positional-only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Falls back to :meth:`~object.__index__` if :meth:`~object.__float__` is not defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -926,17 +976,36 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with the result after successfully reading input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: int(x=0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int(x, base=10) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. class:: int(number=0, /) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int(string, /, base=10) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return an integer object constructed from a number or a string, or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``0`` if no arguments are given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int(123.45) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
123 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('123') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
123 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int(' -12_345\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-12345 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('FACE', 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
64206 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('0xface', 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
64206 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
>>> int('01110011', base=2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
115 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Return an integer object constructed from a number or string *x*, or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``0`` if no arguments are given. If *x* defines :meth:`~object.__int__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`~object.__index__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it returns ``x.__index__()``. If *x* defines :meth:`~object.__trunc__`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it returns ``x.__trunc__()``. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For floating point numbers, this truncates towards zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If *x* is not a number or if *base* is given, then *x* must be a string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If the argument is not a number or if *base* is given, then it must be a string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:class:`bytes`, or :class:`bytearray` instance representing an integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
in radix *base*. Optionally, the string can be preceded by ``+`` or ``-`` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(with no space in between), have leading zeros, be surrounded by whitespace, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -966,7 +1035,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Grouping digits with underscores as in code literals is allowed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*x* is now a positional-only parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The first parameter is now positional-only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Falls back to :meth:`~object.__index__` if :meth:`~object.__int__` is not defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -977,7 +1046,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.. versionchanged:: 3.11 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:class:`int` string inputs and string representations can be limited to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
help avoid denial of service attacks. A :exc:`ValueError` is raised when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the limit is exceeded while converting a string *x* to an :class:`int` or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the limit is exceeded while converting a string to an :class:`int` or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when converting an :class:`int` into a string would exceed the limit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
See the :ref:`integer string conversion length limitation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<int_max_str_digits>` documentation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.