"Decoding unicode is not supported" in unusual situation

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Mar 7 06:42:53 EST 2012


On Wed, 07 Mar 2012 22:18:50 +1100, Ben Finney wrote:

> deets at web.de (Diez B. Roggisch) writes:
> 
>> John Nagle <nagle at animats.com> writes:
>>
>> > I think that somewhere in "suds", they subclass the "unicode" type.
>> > That's almost too cute.
>> >
>> > The proper test is
>> >
>> > 	isinstance(s,unicode)
>>
>> Woot, you finally discovered polymorphism - congratulations!
> 
> If by “discovered” you mean “broke”.
> 
> John, polymorphism entails that it *doesn't matter* whether the object
> inherits from any particular type; it only matters whether the object
> behaves correctly.
> 
> So rather than testing whether the object inherits from ‘unicode’, test
> whether it behaves how you expect – preferably by just using it as
> though it does behave that way.


I must admit that I can't quite understand John Nagle's original post, so 
I could be wrong, but I *think* that both you and Diez have misunderstood 
the nature of John's complaint.

I *think* he is complaining that some other library -- suds? -- has a 
broken test for Unicode, by using:

if type(s) is unicode: ...

instead of

if isinstance(s, unicode): ...

Consequently, when the library passes a unicode *subclass* to the 
tounicode function, the "type() is unicode" test fails. That's a bad bug.

It's arguable that the library shouldn't even use isinstance, but that's 
an argument for another day.


-- 
Steven



More information about the Python-list mailing list