[Python-Dev] Evil isinstance()
Aahz
aahz@pythoncraft.com
Sun, 31 Mar 2002 16:21:46 -0500
On Sun, Mar 31, 2002, Guido van Rossum wrote:
>Alex Martelli:
>>
>> Why would you want your function to break if called with an instance
>> of UserString, say, or a user-defined number type? Smooth
>> polymorphism is high on the list of Python's strong points -- why
>> break it, when you can preserve this excellent quality?
>
> I'm for this; but it's hard to pick the right test in many cases.
> Many types define both __str__ and __int__ -- but either may lose
> information (in the case of a float, these *both* lose information!).
Yup, this is precisely what I'm concerned with. My use case for any
discussion of this subject is my BCD project (which I haven't worked on
in several months, but never mind). I need to be able to accept floats,
ints/longs, and string representations of numbers, all at the maximum
possible precision for each type. I suppose I could create helper
functions called e.g. BCDfromString() and force people to do explicit
conversions, but that seems messy to me -- and it contravenes the way
int()/str()/float() work.
(Alex, IIRC, the last time we had this discussion you agreed that I
didn't have any choice.)
Note that these days with new-style classes the situation is actually
better: inherit from the built-in types, and isinstance() works
correctly.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
Why is this newsgroup different from all other newsgroups?