Issue479698
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2001-11-08 18:53 by anonymous, last changed 2022-04-10 16:04 by admin. This issue is now closed.
Messages (4) | |||
---|---|---|---|
msg7470 - (view) | Author: Nobody/Anonymous (nobody) | Date: 2001-11-08 18:53 | |
I converted a numeric-like class from the "old-style" Python class to the "new-style" Python class by inheriting from "object". Numerical operations like "__mul__" and "__div__" failed with the "new-style" class. After tracking the problem, I found that the "__coerce__" special method was called when performing mixed-type arithmetic, as expected, with the "old-style" class, but not with the "new-style" class. The arithmetic operations failed because the expected type conversion was not performed with the mixed-type arithmetic. As best I can tell, without examining the Python source code, the following is taking place: 1. The documentation for the "__coerce__" special method states that it is invoked in the case of mixed-type arithmetic only if at least one of the arguments in an arithmetic operation is a class instance. 2. An object created with an "old-style" class is of type "instance", as verified by the "type" function, i.e. <type 'instance'> 3. An object created with a "new-style" class is of a different type, as also verified by the "type function, i.e. <class '.....'> Evidently, because of this change in type, the "__coerce__" function is not longer invoked for "new-style" Python classes. Gary H. Loechelt ON Semiconductor |
|||
msg7471 - (view) | Author: Gary H. Loechelt (loechelt) | Date: 2001-11-26 15:59 | |
Logged In: YES user_id=142817 After further looking into this, I am not sure that it is a bug "per se". It may have been the intentionally designed behavior. It is certainly possible for a user to rewrite arithmetic methods (i.e. '__add__') to explicitly call a coerce method. However, in the very least, changing from the "old-style" to the "new-style" class does break existing code that relies upon the '__coerce__' method, and this fact is not very well documented. |
|||
msg7472 - (view) | Author: Guido van Rossum (gvanrossum) * | Date: 2001-12-03 19:09 | |
Logged In: YES user_id=6380 Encouraged by the submittor's comment, I've decided not to fix this. New-style class instances cannot be old-style numbers, and the conversion from old-style class to new-style class will have to involve changing any numeric operators to do their own coercions. I'll leave this as a documentation item, at a lower priority (I'll add something to the docs on the web). |
|||
msg7473 - (view) | Author: Raymond Hettinger (rhettinger) * | Date: 2003-07-12 02:01 | |
Logged In: YES user_id=80475 Fred fixed this in the docs on June 4, 2002. See Doc/ref/ref3.tex 1.190. Marking a closed. Thanks for the bug report and thorough analysis. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-10 16:04:37 | admin | set | github: 35492 |
2001-11-08 18:53:08 | anonymous | create |