[Python-3000] Updated and simplified PEP 3141: A Type Hierarchy for Numbers

Nick Coghlan ncoghlan at gmail.com
Thu May 17 12:56:04 CEST 2007


Talin wrote:
> This really highlights what I think is a problem with dynamic 
> inheritance, and I think that this inconsistency between traditional and 
> dynamic inheritance will eventually come back to haunt us. It has always 
> been the case in the past that for every property of class B, if 
> isinstance(A, B) == True, then A also has that property, either 
> inherited from B, or overridden in A. The fact that this invariant will 
> no longer hold true is a problem in my opinion.
> 
> I realize that there isn't currently a solution to efficiently allow 
> inheritance of properties via dynamic inheritance. As a software 
> engineer, however, I generally feel that if a feature is unreliable, 
> then it shouldn't be used at all. So if I were designing a class 
> hierarchy of ABCs, I would probably make a rule for myself not to define 
> any properties or methods in the ABCs at all, and to *only* use ABCs for 
> type testing via 'isinstance'.

If a class doesn't implement the interface defined by an ABC, you should 
NOT be registering it with that ABC via dynamic inheritance. *That's* 
the bug - the program is claiming that "instances of class A can be 
treated as if they were an instance of B" when that statement is simply 
not true. And without defining an interface, dispatching on the ABC is 
pointless - you don't know whether or not you support the operations 
implied by that ABC because there aren't any defined!

Now, with respect to the number hierarchy, I think building it as a 
vertical stack doesn't really match the way numbers have historically 
worked in Python -  integers and floats, for example, don't implement 
the complex number API:

 >>> (1).real
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'real'
 >>> (1.0).real
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'real'

Given the migration of PEP 3119 to an approach which is friendlier to 
classification after the fact, it's probably fine to simply punt on the 
question of an ABC heirarchy for numbers (as Talin already pointed out).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list