@@ -141,10 +141,11 @@ are always available. They are listed here in alphabetical order.
141
141
See also :func: `format ` for more information.
142
142
143
143
144
- .. class :: bool(x =False)
144
+ .. class :: bool(object =False, / )
145
145
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
148
149
or omitted, this returns ``False ``; otherwise, it returns ``True ``. The
149
150
:class: `bool ` class is a subclass of :class: `int ` (see :ref: `typesnumeric `).
150
151
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.
153
154
.. index :: pair: Boolean; type
154
155
155
156
.. versionchanged :: 3.7
156
- * x * is now a positional-only parameter .
157
+ The parameter is now positional-only.
157
158
158
159
.. function :: breakpoint(*args, **kws)
159
160
@@ -371,29 +372,73 @@ are always available. They are listed here in alphabetical order.
371
372
support for top-level ``await ``, ``async for ``, and ``async with ``.
372
373
373
374
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:
376
416
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")
385
421
422
+ If the argument is a number, the constructor serves as a numeric
423
+ conversion like :class: `int ` and :class: `float `.
386
424
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
389
429
to :meth: `~object.__index__ `.
390
430
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.
392
440
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 ``.
397
442
398
443
The complex type is described in :ref: `typesnumeric `.
399
444
@@ -662,21 +707,38 @@ are always available. They are listed here in alphabetical order.
662
707
elements of *iterable * for which *function * is false.
663
708
664
709
665
- .. class :: float(x=0.0)
710
+ .. class :: float(number=0.0, /)
711
+ float(string, /)
666
712
667
713
.. index ::
668
714
single: NaN
669
715
single: Infinity
670
716
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
672
733
673
734
If the argument is a string, it should contain a decimal number, optionally
674
735
preceded by a sign, and optionally embedded in whitespace. The optional
675
736
sign may be ``'+' `` or ``'-' ``; a ``'+' `` sign has no effect on the value
676
737
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:
680
742
681
743
.. productionlist :: float
682
744
sign: "+" | "-"
@@ -685,9 +747,10 @@ are always available. They are listed here in alphabetical order.
685
747
digit: <a Unicode decimal digit, i.e. characters in Unicode general category Nd>
686
748
digitpart: `digit ` (["_"] `digit `)*
687
749
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 `
691
754
692
755
Case is not significant, so, for example, "inf", "Inf", "INFINITY", and
693
756
"iNfINity" are all acceptable spellings for positive infinity.
@@ -703,26 +766,13 @@ are always available. They are listed here in alphabetical order.
703
766
704
767
If no argument is given, ``0.0 `` is returned.
705
768
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
-
719
769
The float type is described in :ref: `typesnumeric `.
720
770
721
771
.. versionchanged :: 3.6
722
772
Grouping digits with underscores as in code literals is allowed.
723
773
724
774
.. versionchanged :: 3.7
725
- * x * is now a positional-only parameter .
775
+ The parameter is now positional-only.
726
776
727
777
.. versionchanged :: 3.8
728
778
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.
906
956
with the result after successfully reading input.
907
957
908
958
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
911
981
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__ `,
916
985
it returns ``x.__trunc__() ``.
917
986
For floating point numbers, this truncates towards zero.
918
987
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,
920
989
:class: `bytes `, or :class: `bytearray ` instance representing an integer
921
990
in radix *base *. Optionally, the string can be preceded by ``+ `` or ``- ``
922
991
(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.
946
1015
Grouping digits with underscores as in code literals is allowed.
947
1016
948
1017
.. versionchanged :: 3.7
949
- * x * is now a positional-only parameter .
1018
+ The first parameter is now positional-only.
950
1019
951
1020
.. versionchanged :: 3.8
952
1021
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.
957
1026
.. versionchanged :: 3.11
958
1027
:class: `int ` string inputs and string representations can be limited to
959
1028
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
961
1030
when converting an :class: `int ` into a string would exceed the limit.
962
1031
See the :ref: `integer string conversion length limitation
963
1032
<int_max_str_digits>` documentation.
0 commit comments