summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-30 20:44:20 (GMT)
committerGitHub <noreply@github.com>2024-05-30 20:44:20 (GMT)
commit844b535c51201a176865cea059533bf13a3826e2 (patch)
treeb824eeedd92f7f8bda5b2fd15789acbeff2ee49b /Objects
parentd6bede815dd88adb9a89eda15c01573fd03af080 (diff)
downloadcpython-844b535c51201a176865cea059533bf13a3826e2.zip
cpython-844b535c51201a176865cea059533bf13a3826e2.tar.gz
cpython-844b535c51201a176865cea059533bf13a3826e2.tar.bz2
[3.13] gh-109218: Improve documentation for the complex() constructor (GH-119687) (GH-119803)
* 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 ec1ba264607b2b7b98d2602f5536a1d02981efc6) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/clinic/complexobject.c.h9
-rw-r--r--Objects/complexobject.c9
2 files changed, 12 insertions, 6 deletions
diff --git a/Objects/clinic/complexobject.c.h b/Objects/clinic/complexobject.c.h
index 49b5030..46c3b35 100644
--- a/Objects/clinic/complexobject.c.h
+++ b/Objects/clinic/complexobject.c.h
@@ -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]*/
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index d8b0e84..1aa3960 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -911,14 +911,17 @@ complex.__new__ as complex_new
real as r: object(c_default="NULL") = 0
imag as i: object(c_default="NULL") = 0
-Create a complex number from a real part and an optional imaginary part.
+Create a complex number from a string or numbers.
-This is equivalent to (real + imag*1j) where imag defaults to 0.
+If a string is given, parse it as a complex number.
+If a single number is given, convert it to a complex number.
+If the 'real' or 'imag' arguments are given, create a complex number
+with the specified real and imaginary components.
[clinic start generated code]*/
static PyObject *
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
-/*[clinic end generated code: output=b6c7dd577b537dc1 input=f4c667f2596d4fd1]*/
+/*[clinic end generated code: output=b6c7dd577b537dc1 input=ff4268dc540958a4]*/
{
PyObject *tmp;
PyNumberMethods *nbr, *nbi = NULL;