You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following is quoted from the Python docs (ref/numeric_types):
"Note: If the right operand's type is a subclass of the left operand's
type and that subclass provides the reflected method for the operation,
this method will be called before the left operand's non-reflected
method. This behavior allows subclasses to override their ancestors'
operations."
My issue is that the built-in complex type does not respect this rule
(see code below). Note that float can be subclassed using the method
shown below and the rules are applied correctly. It seems that it is
only a problem with complex.
"""In the current implementation, the built-in numeric types int, long and
float do not use coercion; the type complex however does use it. The
difference can become apparent when subclassing these types. Over time,
the type complex may be fixed to avoid coercion. [...]"""
In the OPs example, when z + xz is evaluated, xz is (successfully) coerced
to type complex, and then complex.__add__ is called to do the addition as
usual. There's definitely (at least) a documentation bug here, given
that at the same place as referenced above it says:
"""New-style classes (those derived from object) never invoke the
__coerce__() method in response to a binary operator; the only time
__coerce__() is invoked is when the built-in function coerce() is
called."""
At the level of the C code, the practical difference between complex and
(for example) float is that the float type has Py_TPFLAGS_CHECKTYPES set,
and all arithmetic operations do their own conversions. The complex type
doesn't set Py_TPFLAGS_CHECKTYPES, and so (contrary to the documentation)
relies on complex_coerce to do any conversions.
While Mark Dickinson's patch fixes the documentation, it does not offer
a solution to the original problem, which was rooted in a need to
provide special behaviour based on the numeric types. I made the
original posting because I hoped that this problem could be resolved.
[...] does not offer
a solution to the original problem, which was rooted in a need to
provide special behaviour based on the numeric types. I made the
original posting because I hoped that this problem could be resolved.
Activity
gumtree commentedon Aug 29, 2008
The following is quoted from the Python docs (ref/numeric_types):
"Note: If the right operand's type is a subclass of the left operand's
type and that subclass provides the reflected method for the operation,
this method will be called before the left operand's non-reflected
method. This behavior allows subclasses to override their ancestors'
operations."
My issue is that the built-in complex type does not respect this rule
(see code below). Note that float can be subclassed using the method
shown below and the rules are applied correctly. It seems that it is
only a problem with complex.
print type(z + xz) # gives complex when xcomplex is required
devdanzin commentedon Feb 10, 2009
Confirmed in trunk. Here's a copy-n-past'able testcase:
print type(z + xz)
print type(f + xf)
mdickinson commentedon Feb 10, 2009
I'll take a look.
mdickinson commentedon Feb 10, 2009
So there's a hint about what's happening here at the bottom of the section
http://docs.python.org/reference/datamodel.html#id5
of the docs, where it says:
"""In the current implementation, the built-in numeric types int, long and
float do not use coercion; the type complex however does use it. The
difference can become apparent when subclassing these types. Over time,
the type complex may be fixed to avoid coercion. [...]"""
In the OPs example, when z + xz is evaluated, xz is (successfully) coerced
to type complex, and then complex.__add__ is called to do the addition as
usual. There's definitely (at least) a documentation bug here, given
that at the same place as referenced above it says:
"""New-style classes (those derived from object) never invoke the
__coerce__() method in response to a binary operator; the only time
__coerce__() is invoked is when the built-in function coerce() is
called."""
At the level of the C code, the practical difference between complex and
(for example) float is that the float type has Py_TPFLAGS_CHECKTYPES set,
and all arithmetic operations do their own conversions. The complex type
doesn't set Py_TPFLAGS_CHECKTYPES, and so (contrary to the documentation)
relies on complex_coerce to do any conversions.
mdickinson commentedon Feb 10, 2009
I think this issue comes down to a doc fix, though I've opened bpo-5211
for anyone who fancies having a go at removing coercion from the complex
type.
Here's a doc patch.
gumtree commentedon Feb 11, 2009
While Mark Dickinson's patch fixes the documentation, it does not offer
a solution to the original problem, which was rooted in a need to
provide special behaviour based on the numeric types. I made the
original posting because I hoped that this problem could be resolved.
mdickinson commentedon Feb 11, 2009
[gumtree]
Please see bpo-5211 for further discussion.
birkenfeld commentedon Feb 13, 2009
Applied doc patch in r69573.