Skip to content

Fix complex type to avoid coercion in 2.7. #49461

Closed

Description

@mdickinson
BPO 5211
Nosy @mdickinson, @meadori
Files
  • issue-5211-patch: patch against 2.7 trunk
  • unnamed
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/mdickinson'
    closed_at = <Date 2010-02-21.12:58:44.750>
    created_at = <Date 2009-02-10.23:19:22.161>
    labels = ['interpreter-core', 'easy', 'type-feature']
    title = 'Fix complex type to avoid coercion in 2.7.'
    updated_at = <Date 2010-11-28.22:40:11.928>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2010-11-28.22:40:11.928>
    actor = 'gumtree'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2010-02-21.12:58:44.750>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2009-02-10.23:19:22.161>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['16148', '19861']
    hgrepos = []
    issue_num = 5211
    keywords = ['easy']
    message_count = 25.0
    messages = ['81612', '81633', '81694', '98714', '98784', '98798', '98810', '98811', '98828', '98829', '98830', '98941', '98971', '99653', '99691', '99697', '106757', '122432', '122453', '122472', '122499', '122659', '122724', '122727', '122742']
    nosy_count = 3.0
    nosy_names = ['mark.dickinson', 'gumtree', 'meador.inge']
    pr_nums = []
    priority = 'low'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5211'
    versions = ['Python 2.7']

    Activity

    1. mdickinson commented on Feb 10, 2009

      @mdickinson
      MemberAuthor

      In the 'coercion rules' section of the reference manual, at:

      http://docs.python.org/dev/reference/datamodel.html#id5

      it says:

      """Over time, the type complex may be fixed to avoid coercion."""

      In 3.x, the complex type has (necessarily) been fixed to avoid coercion,
      and it ought to be a fairly easy task to backport that fix to 2.7, for
      someone who wants to get his or her feet wet with some CPython hacking.

      As far as I can see, there's no great benefit in such a change, except
      that the presence of coercion for the complex type causes confusion
      occasionally: see bpo-3734 for an example of this.

    2. added
      interpreter-core
      type-feature
      on Feb 10, 2009
    3. mdickinson commented on Feb 11, 2009

      @mdickinson
      MemberAuthor

      Comment by gumtree copied from bpo-3734 discussion:

      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.

      gumtree, would you be interested in working on a patch for this feature-
      request? As mentioned above, the necessary changes are already present
      in 3.x, so all that's entailed is figuring out which bits of the
      Object/complexobject.c in the py3k source need to be transferred to the
      trunk, and making sure that everything's properly tested.

      Alternatively, could you explain in a little more detail why this change
      is important to you? Subclassing complex doesn't seem like a very
      common thing to want to do, so I'm curious about your use case.

    4. gumtree commented on Feb 12, 2009

      gumtreemannequin
      Mannequin

      I am happy to collaborate in finding a solution, but I do not have
      enough knowledge or skill to work with the code alone.

      Simply documenting it does not remove the frustration that a few people
      will enounter. The key point being that you can subclass the other
      numeric types, but not complex. Worse, you may think that you have
      succeeded in subclassing complex too, because it is only when the
      __ropn__ binary operators are used that the type reverts to the base
      class type.

      Would you want to subclass a numeric type? I agree, it is a bit obsure,
      but I did a search on this before making the post and there have been
      others who found the problem too.

      In my case, I think that the motivation may seem a bit obscure. I had
      developed some numerical-like types (from scratch -- no subclassing)
      and I wanted to be able to write functions taking as possible arguments
      these types and Python numerical types indifferently. I realised that I
      could not achieve exactly what I wanted, however, by subclassing float,
      int, etc I could add a few methods that would allow my generic
      functions to work with either my types or the subclassed Python types.
      At the same time, the subclassed numerical types could still be used as
      numerical quantities (float, int,...). It seemed like a pretty elegant
      solution.

      If that explanation does not make sense, then I suppose other simpler
      motivations could be, eg, to subclass float so that only positive
      values are acceptable; to subclass complex so that only values lying
      within the unit circle are acceptable, etc. That is, one might like to
      define a type that can only take on physically meaningful values (mass
      cannot be negative, a complex reflection coeffcieint cannot have a
      magnitude greater than unity, ..)

      So, my feeling is that this is worth fixing because the work done on
      float, int etc, is clearly useful and it appears (to me) that the
      complex case is an oversight.

    5. meadori commented on Feb 2, 2010

      @meadori
      Member

      Mark,

      Is this still of interest?

      I found the relevant changes in py3k, but I am not sure it is the behavior that gumtree is expecting. Since py3k removes coercion completely, the test case from bpo-3734 would just issue:

         Traceback (most recent call last):
           File "test-5211.py", line 34, in <module>
             print type(z + xz)
           File "test-5211.py", line 5, in __coerce__
             t = complex.__coerce__(self,other)
         AttributeError: type object 'complex' has no attribute '__coerce__'

      Where as I think gumtree wants the xcomplex case to behave as the xfloat case, e.g.
      <class '__main__.xfloat'>
      <class '__main__.xcomplex'>
      Since coercion is getting axed in py3k, I don't think it makes sense to provide this behavior.

      On the other hand, as you mentioned, the removal of coercion from complex could be backported. However, if we are going to do that then we might as well just backport the whole removal of coercion instead of just the bits from 'complexobject.c'. Bascially checkins r51431 and r58226.

    6. mdickinson commented on Feb 3, 2010

      @mdickinson
      MemberAuthor

      Yes, I'd certainly be interested in reviewing a patch. Though the current behaviour is at most a minor wart, and since it's gone in Python 3.x the motivation to fix it isn't huge. :)

      Where as I think gumtree wants the xcomplex case to behave as
      the xfloat case, e.g. ...

      Yes, that was what I was proposing. But as you point out, the new behaviour wouldn't even match the behaviour of Python 3.x, so it really wouldn't be a terribly useful change.

      However, if we are going to do that then we might as well just
      backport the whole removal of coercion.

      That's not really an option: it has the potential to break existing code that uses coercion. Removing coercion in Python 2.x would require at least a PEP plus one version's worth of DeprecationWarning. And given that it currently looks as though Python 2.8 isn't likely to happen at all, that would likely be a wasted effort.

    7. gumtree commented on Feb 3, 2010

      gumtreemannequin
      Mannequin

      I also agree that this bug was never more than a small wart. However, I'm now curious.

      If Python 3 does not support coercion, I think that it will not be possible to write something like my xfloat class, derived from float (i.e., some binary operations between an xfloat and a float would return a float instead of an xfloat).

      If I am correct in think that it would seem to be a step backward.

      Will Python 3 deal with mixed types in some other way, or has this problem been abandoned altogether? If it is the latter, I think it is a pity.

    8. meadori commented on Feb 4, 2010

      @meadori
      Member

      Mark

      Yes, that was what I was proposing.

      As you pointed out in bpo-3734, the patch is basically:

      ===================================================================

      --- Objects/complexobject.c	(revision 77909)
      +++ Objects/complexobject.c	(working copy)
      @@ -1282,7 +1282,8 @@
       	PyObject_GenericGetAttr,		/* tp_getattro */
       	0,					/* tp_setattro */
       	0,					/* tp_as_buffer */
      -	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
      +	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES 
      +                | Py_TPFLAGS_BASETYPE,          /* tp_flags */
       	complex_doc,				/* tp_doc */
       	0,					/* tp_traverse */
       	0,					/* tp_clear */

      plus the relevant test cases and documentation changes.

      Mark

      That's not really an option: it has the potential to break existing
      code that uses coercion. Removing coercion in Python 2.x would
      require at least a PEP plus one version's worth of DeprecationWarning.

      Ah, good point. In that case I am -1 on changing the behavior at all. For the sole reason that cases like Blair originally reported would not work as expected in version < 2.7, then would work in 2.7, and then would break again in > 3.0. I think explaining and documenting the change in behavior would be really confusing.

      Blair

      If I am correct in think that it would seem to be a step backward.

      I disagree. I think type coercion (or implicit conversions) is hard to follow and understand. I say this from many years of C and C++ programming. I see bugs all of the time in these languages due to conversions silently happening that the programmer was not aware of. Yes the feature is convenient, but documenting and explaining these rules to programmers becomes very tedious and requires that most programmers have deep understanding of the languages typing rules. This typically leads to books like "Effective C++" that catalog a list of things *not* to do in a language. The relevant case for C++ being to avoid user defined conversions and use explicit constructers. As such, I am definitely +1 on removing coercion.

    9. gumtree commented on Feb 4, 2010

      gumtreemannequin
      Mannequin

      OK. I have gone back to the beginning to refresh my memory and I see a possible point of misunderstanding. I am not sure that we are really talking about the problem that prompted my initial report (msg72169, bpo-3734).

      Immediately following my message, Daniel Diniz confirmed the bug and expanded on my code with an xfloat class of his own that uses __coerce__.

      In fact, if I had submitted an xfloat class it would have been the following

      class xfloat( float ):
      
          def __new__(cls,*args,**kwargs):
              return float.__new__(cls,*args,**kwargs)
              
          def __add__(self,x):
              return xfloat( float.__add__(self,x) )
      
          def __radd__(self,x):
              return xfloat( float.__radd__(self,x) )

      My xfloat works fine in 2.6.4 and it was my wish, at the time, to write a class for xcomplex that behaved in a similar way. According to the Python manual, that should have been possible, but it wasn't.

      So, I guess coercion is not really the problem.

      However, there does seem to be something wrong with the complex type.

      I have looked at the manual for Python 3 and see that the same rules apply for classes that emulate numeric types, namely:

      "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."

      The question I have then is will the following work in Python 3 (it doesn't in 2.6.4)?

      class xcomplex( complex ):
      
          def __new__(cls,*args,**kwargs):
              return complex.__new__(cls,*args,**kwargs)
      
      ##    def __coerce__(self,other):
      ##        t = complex.__coerce__(self,other)
      ##        try:
      ##            return (self,xcomplex(t[1]))
      ##        except TypeError:
      ##            return t
              
          def __add__(self,x):
              return xcomplex( complex.__add__(self,x) )
          
          def __radd__(self,x):
              return xcomplex( complex.__radd__(self,x) )
      
      xz = xcomplex(1+2j)
      xy = float(10.0)
      z = complex(10+1j)

      print "would like xcomplex type each time"
      print type(xz + z)
      print type(xz + xy)
      print type(xz + 10)
      print type(xy + xz)
      print type(10 + xz)
      print type(z + xz)

    10. mdickinson commented on Feb 4, 2010

      @mdickinson
      MemberAuthor

      Blair: I don't think you'll have any problems getting the behaviour you in Python 3. For example:

      Python 3.2a0 (py3k:77952, Feb  4 2010, 10:56:12) 
      [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> class xcomplex(complex):
      ...     def __add__(self, other): return xcomplex(complex(self) + other)
      ...     __radd__ = __add__
      ... 
      >>> xz = xcomplex(1+2j)
      >>> all(type(xz + y) is type(y + xz) is xcomplex for y in (1, 10.0, 10+1j, xz))
      True

      So I don't consider that the removal of coerce and the __coerce__ magic method is a step backward: it still allows mixed-type operations, but without coerce the rules for those operations are significantly cleaner.

      The real problem case in 2.6.4 seems to be when doing <instance of complex> + <instance of xcomplex>: in this case, Python first calls complex.__coerce__ (which returns its arguments unchanged), then complex.__add__. None of the xcomplex methods even gets a look in, so there's no opportunity to force the return type to be xcomplex.

      If you look at the source (see particularly the binary_op1 function in Objects/abstract.c ) you can see where this behaviour is coming from. The complex type (along with its subclasses) is classified as an 'old-style' number, while ints, longs and floats are 'new-style' (note that this has nothing to do with the distinction between old-style and new-style classes). Operations between new-style numbers use the scheme described in the documentation, but where old-style numbers are involved there's an extra coercion step. In particular, when adding a complex to an xcomplex, the rule you quoted (about the case when the right operand is an instance of a subclass of the class of the left operand) isn't applied.

      It's too risky to change the general behaviour for old-style numbers: I don't think there are any old-style numbers besides complex in the Python code, but there may well be some in third party extensions. Certainly documenting it better would be an option, and making complex a new-style number for Python 2.7 seems like a reasonable thing to consider.

    11. mdickinson commented on Feb 4, 2010

      @mdickinson
      MemberAuthor

      As you pointed out in bpo-3734, the patch is basically:
      <snipped patch that adds Py_TPFLAGS_CHECKTYPES>

      Yes, that's the essence of it. In addition, each of the functions implementing a complex special method would need to do its own argument conversion. (Compare the implementation of complex_add in py3k with that in trunk.)

    12. mdickinson commented on Feb 4, 2010

      @mdickinson
      MemberAuthor

      Yes, that was what I was proposing. But as you point out, the new
      behaviour wouldn't even match the behaviour of Python 3.x, so it really
      wouldn't be a terribly useful change.

      Hmm. I take this back: if complex were made 'new-style' in 2.7, then it *would* be possible to write fairly obvious code (not using coerce or __coerce__) that operated in the same way in both 2.7 and 3.2. So I still think it's worth considering.

    13. meadori commented on Feb 6, 2010

      @meadori
      Member

      if complex were made 'new-style' in 2.7, then it *would* be possible to > write fairly obvious code (not using coerce or __coerce__) that operated > in the same way in both 2.7 and 3.2. So I still think it's worth
      considering.

      Agreed. I have attached a patch with src, test, and doc changes.

    14. 1 remaining item

    15. self-assigned this
      on Feb 6, 2010
    16. mdickinson commented on Feb 21, 2010

      @mdickinson
      MemberAuthor

      Apologies for the delay; tomorrow was a long time coming...

      The patch looks great---thank you! I added a ".. versionchanged" note to the documentation, and fixed a couple of whitespace issues; apart from that I didn't change anything. Applied in r78280.

    17. mdickinson commented on Feb 21, 2010

      @mdickinson
      MemberAuthor

      I must have been operating on autopilot; not only did I forget to put the issue number in the checkin message, but I forgot to acknowledge Meador Inge for the patch. Fixed now, with apologies to Meador.

    18. meadori commented on Feb 22, 2010

      @meadori
      Member

      I added a ".. versionchanged" note to the documentation, and fixed a
      couple of whitespace issues;

      Thanks. I checked out the changes you made so that I will know what to do next time :).

      Fixed now, with apologies to Meador.

      No worries. Thanks for applying the patch!

    19. mdickinson commented on May 30, 2010

      @mdickinson
      MemberAuthor

      r78280 didn't remove the implicit coercion for rich comparisons; that's now been done in r81606.

    20. gumtree commented on Nov 26, 2010

      gumtreemannequin
      Mannequin

      Hi Mark,

      I thought that this had all been fixed, but it seems not.

      Consider the following:

      class xcomplex( complex ):
          def __new__(cls,*args,**kwargs):
              return complex.__new__(cls,*args,**kwargs)
          def __add__(self,x):
              return xcomplex( complex.__add__(self,x) )
          def __radd__(self,x):
              print "larg: ", type(x),"returning: ",
              return xcomplex( complex.__radd__(self,x) )
      
      class xfloat(float):
          def __new__(cls,*args,**kwargs):
              return float.__new__(cls,*args,**kwargs)
          def __add__(self,x):
              return xfloat( float.__add__(self,x) )
          def __radd__(self,x):
              print "larg: ", type(x),"returning: ",
              return xfloat( float.__radd__(self,x) )
      
      z = 1j
      xz = xcomplex(1j)
      f = 1.0
      xf = xfloat(1.0)

      print
      print "-----------"
      print "expect xcomplex:", type(z + xz)
      print "expect xcomplex:", type(f + xz)
      print "expect xfloat:", type(f + xf)
      print "expect ???:", type(z + xf)

      When this runs, the first three conversions are fine, the last is not: there
      is no call to xfloat.__radd__. It seems that the builtin complex type simply
      thinks it is dealing with a float. Here is the output

      -----------
      expect xcomplex: larg: <type 'complex'> returning: <class
      '__main__.xcomplex'>
      expect xcomplex: larg: <type 'float'> returning: <class
      '__main__.xcomplex'>
      expect xfloat: larg: <type 'float'> returning: <class '__main__.xfloat'>
      expect ???: <type 'complex'>

      The last line shows that no call to __radd__ occurred.

      Is there anything that can be done now about this now, or is it just too
      late?

      Regards

      Blair

      At 01:13 a.m. 31/05/2010, you wrote:

      Mark Dickinson <dickinsm@gmail.com> added the comment:

      r78280 didn't remove the implicit coercion for rich comparisons; that's now
      been done in r81606.

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue5211\>
      _______________________________________Python tracker <
      report@bugs.python.org>

    21. mdickinson commented on Nov 26, 2010

      @mdickinson
      MemberAuthor

      I think that's expected behaviour. Note that int vs float behaves in the same way as float vs complex:

      >>> class xint(int):
      ...     def __radd__(self, other):
      ...         print "__radd__"
      ...         return 42
      ... 
      >>> 3 + xint(5)
      __radd__
      42
      >>> 3.0 + xint(5)  # xint.__radd__ not called.
      8.0

      As with your example, the float.__add__ method is happy to deal with an int or an instance of any subclass of int.

    22. gumtree commented on Nov 26, 2010

      gumtreemannequin
      Mannequin

      I see your point Mark, however it does not seem to be the right way to do
      this.

      Are you aware that Python has formally specified this behaviour somewhere? I
      could not find an explicit reference in the documentation.

      The problem that has been fixed is covered in the documentation:

      (3.4.8. Emulating 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.)

      This rule is needed so that mixed-type arithmetic operations do not revert
      to the ancestor's type. However, one would expect that different numeric
      types (int float complex) would all behave in a similar way. For example,

      xi = xint(3)
      3 + xi  # is an xint(6)
      3.0 + xi # is float(6)

      This is the same problem as the one that has been fixed from a practical
      point of view. Such behaviour is not going to be useful (IMO).

      It seems to me that xint.__radd__ would need to be called if the left
      operand is a subclass of any of the number types (in this case,
      isinstance(left_op,numbers.Complex) == True).

      Am I missing something?

      Mark Dickinson <dickinsm@gmail.com> added the comment:

      I think that's expected behaviour. Note that int vs float behaves in the
      same way as float vs complex:

      ...     def __radd__(self, other):
      ...         print "__radd__"
      ...         return 42
      ...
      >>> 3 + xint(5)
      __radd__
      42
      >>> 3.0 + xint(5)  # xint.__radd__ not called.
      8.0

      As with your example, the float.__add__ method is happy to deal with an int
      or an instance of any subclass of int.

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue5211\>


    23. gumtree commented on Nov 27, 2010

      gumtreemannequin
      Mannequin

      I'd like to add a few more observations to the mix.

      I have run the following in both 2.6.6 and in 2.7

      class xfloat(float):
          def __new__(cls,x):
              return float.__new__(cls,x)
      
          def __radd__(self,lhs):
              print "__radd__ got: %s" % type(lhs)
              if isinstance(lhs,(int,float)):
                  return xfloat( float(self) + lhs )
              else:
                  return NotImplemented
      
      xf = xfloat(9.0)
      
      cases = dict(int=1,float=1.0,complex=1.0+1j)
      for k,v in cases.items():
          y = v + xf
          print "%s + xfloat" % k
          print type(y)
          print y

      In 2.7 this gives:

      __radd__ got: <type 'int'>
      int + xfloat
      <class '__main__.xfloat'>
      10.0
      __radd__ got: <type 'float'>
      float + xfloat
      <class '__main__.xfloat'>
      10.0
      complex + xfloat
      <type 'complex'>
      (10+1j)

      In 2.6.6 I get:

      __radd__ got: <type 'int'>
      int + xfloat
      <class '__main__.xfloat'>
      10.0
      __radd__ got: <type 'float'>
      float + xfloat
      <class '__main__.xfloat'>
      10.0
      __radd__ got: <type 'complex'>
      complex + xfloat
      <type 'complex'>
      (10+1j)

      They are the same except for the last case.

      My feeling is that the behaviour of 2.6.6 (for subclassing float) is
      correct.

      The behaviour of 2.6.6 is needed to enable you to implement the commutative
      property of addition (ie, you expect to get the same outcome from x+y or
      y+x), which I would say is a pretty fundamental requirement.

      I have also tried the following

      class xint(int):
          def __new__(cls,x):
              return int.__new__(cls,x)
      
          def __radd__(self,lhs):
              print "__radd__ got: %s" % type(lhs)
              if isinstance(lhs,(int,)):
                  return xint( float(self) + lhs )
              else:
                  return NotImplemented

      print
      print "-------------------"
      xf = xint(9)

      cases = dict(int=1,float=1.0,complex=1.0+1j)
      for k,v in cases.items():
          y = v + xf
          print "%s + xint" % k
          print type(y)
          print y

      In 2.6.6 I get

      __radd__ got: <type 'int'>
      int + xint
      <class '__main__.xint'>
      10
      float + xint
      <type 'float'>
      10.0
      __radd__ got: <type 'complex'>
      complex + xint
      <type 'complex'>
      (10+1j)

      and in 2.7

      -------------------
      __radd__ got: <type 'int'>
      int + xint
      <class '__main__.xint'>
      10
      float + xint
      <type 'float'>
      10.0
      complex + xint
      <type 'complex'>
      (10+1j)

      In my opinion, 2.6.6 was faulty in the float + xint case, for the same
      reasons as above, and 2.7 is faulty in both float + xint and complex + xint.

    24. mdickinson commented on Nov 28, 2010

      @mdickinson
      MemberAuthor

      In my opinion, 2.6.6 was faulty in the float + xint case, for the same
      reasons as above, and 2.7 is faulty in both float + xint and complex +
      xint.

      Well, I disagree: Python is behaving as designed and documented in these cases. If you want to argue that the *design* decisions are the wrong ones, then I'd suggest opening a discussion on the python-ideas mailing list, where more people are likely to get involved---this tracker isn't really the right place for that sort of discussion.

      Leaving complex out of the mix for the moment, it sounds to me as though you'd like, e.g.,

      <float> + <subclass of int>

      to call the int subclass's __radd__ method (if it exists) before calling the float's __add__ method. Is that correct?

      Or are you suggesting that float's __add__ method shouldn't accept instances of subclasses of int at all? (i.e., that float.__add__ should return NotImplemented when given an instance of xint).

      In the first case, you need to come up with general semantics that would give you the behaviour you want for float and xint---e.g., when given numeric objects x and y, what general rule should Python apply to decide whether to call x.__add__ or y.__radd__ first?

      In the second case, I'd argue that you're going against the whole idea of object-oriented programming; by making xint a subclass of int, you're declaring that its instances *are* 'ints' in a very real sense, so it's entirely reasonable for float's __add__ method to accept them.

      In either case, note that Python 2.x is not open for behaviour changes, only for bugfixes. Since this isn't a bug (IMO), such changes could only happen in 3.x.

      Please take further discussion to the python-ideas mailing list.

    25. gumtree commented on Nov 28, 2010

      gumtreemannequin
      Mannequin

      Just to keep this discussion as clear as possible Mark, it was your first
      option that I suggest is needed.

      When that is done (as it was for a subclass of float in 2.6.6) it is
      possible for the author of the subclass to implement commutative binary
      operations (like + and * that must behave the same regardless of argument
      order). Otherwise (as far as I can see) this cannot be done.

      On Mon, Nov 29, 2010 at 5:04 AM, Mark Dickinson <report@bugs.python.org>wrote:

      Changes by Mark Dickinson <dickinsm@gmail.com>:

      Removed file: http://bugs.python.org/file19820/unnamed


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue5211\>


    26. mdickinson commented on Nov 28, 2010

      @mdickinson
      MemberAuthor

      Just to keep this discussion as clear as possible Mark, it was your
      first option that I suggest is needed.

      Okay, so you want <float instance> + <xint instance> to try xint.__radd__ before float.__add__.

      How do you propose this be achieved? Can you explain exactly what semantics you'd like to see? You've indicated what you want to happen for float and xint, but how should Python behave in general here?

      In particular, when evaluating 'x + y' for general Python objects x and y, what rule or rules should Python use to decide whether to try x.__add__(y) or y.__radd__(x) first?

      It seems you want some general mechanism that results in xint being 'preferred' to float, in the sense that xint.__radd__ and xint.__add__ will be tried in preference to float.__add__ and float.__radd__ (respectively). But it's not clear to me what criterion Python would use to determine which out of two types (neither one inheriting from the other) should be 'preferred' in this sense.

    27. gumtree commented on Nov 28, 2010

      gumtreemannequin
      Mannequin

      I am not really the person (I don't know how Python is implemented) to
      explain how the correct behaviour should be achieved (sorry). I do
      appreciate that this may seem like exceptional behaviour. Numbers are a bit
      different.

      However, for what its worth, I think that the 'correct behaviour' was
      implemented for subclasses of float and was working in Python 2.6, but not
      now in 2.7. I don't know how the earlier implementation was done, but it
      does work (I have used it to develop a nice little math library). Would
      there be any documentation about the implementation?

      I would say that the semantics do not need to apply to arbitrary Python
      objects. The problem arises for numeric type subclasses when they are mixed
      with non-subclassed numeric types.

      In that case:

      For 'x opn y' any binary operator (like +,*, etc), if (and only if) 'x' is
      a built-in numeric type (int, long, float, complex) and 'y' is a subclass of
      a built-in numeric type, then y.__ropn__(x) (if it exists) should be called
      before x.__opn__(y).

      If that were done, then subclasses of number types can implement commutative
      operator properties. Otherwise, I don't think it works properly.

      I see this as 'special' behaviour required of the int, long, float and
      complex classes, rather than special behaviour for all Python objects.

      If both 'x' and 'y' are subclasses of built in number types the required
      behaviour seems too complicated to specify. I would be inclined to do
      nothing special. That is, if both 'x' and 'y' are derived from built in
      numeric classes (possibly different types) then x.__opn__(y) would be
      called.

      And should this apply to non-number types? I think not. Numbers deserve
      special treatment.

      I hope this helps.

      On Mon, Nov 29, 2010 at 9:48 AM, Mark Dickinson <report@bugs.python.org>wrote:

      Changes by Mark Dickinson <dickinsm@gmail.com>:

      Removed file: http://bugs.python.org/file19859/unnamed


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue5211\>


    28. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    easyinterpreter-coretype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions