gh-109218: Improve documentation for the complex() constructor (GH-119687)

* 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.)
This commit is contained in:
Serhiy Storchaka 2024-05-30 23:20:07 +03:00 committed by GitHub
parent 1c04c63ced
commit ec1ba26460
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 135 additions and 63 deletions

View file

@ -94,9 +94,12 @@ PyDoc_STRVAR(complex_new__doc__,
"complex(real=0, imag=0)\n"
"--\n"
"\n"
"Create a complex number from a real part and an optional imaginary part.\n"
"Create a complex number from a string or numbers.\n"
"\n"
"This is equivalent to (real + imag*1j) where imag defaults to 0.");
"If a string is given, parse it as a complex number.\n"
"If a single number is given, convert it to a complex number.\n"
"If the \'real\' or \'imag\' arguments are given, create a complex number\n"
"with the specified real and imaginary components.");
static PyObject *
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);
@ -157,4 +160,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=04e6261649967b30 input=a9049054013a1b77]*/
/*[clinic end generated code: output=295ecfd71389d7fe input=a9049054013a1b77]*/