[Python-ideas] duck typing for io write methods

Nick Coghlan ncoghlan at gmail.com
Thu Jun 13 15:23:38 CEST 2013


On 13 June 2013 23:09, Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
> Nick Coghlan <ncoghlan at ...> writes:
>
>>
>> On 13 June 2013 22:46, Wolfgang Maier
>> <wolfgang.maier at ...> wrote:
>> > ok, that's a nice technical explanation, but is that reasonable behavior?
>> > Shouldn't Python check first to see if the object defines __bytes__() before
>> > defaulting to the other options?
>>
>> It doesn't matter whether it's reasonable at this point, it can't be
>> changed without breaking backwards compatibility. Those things are
>> special cased, and it's not worth the hassle of changing it.
>>
>> Cheers,
>> Nick.
>>
>
> I'm confused. Why would it break backwards compatibility, when the default
> behaviour is the same? As far as I can see, it would only affect code that
> defines a __bytes__ method for subclasses of str or int, but then relies on
> them never actually getting called ??

A good point. That reduces it to arguing for slowing down the common
cases to check for an incredibly niche case, which is still a hard
sell (just not as hard a sell as breaking backwards compatibility).

Inheriting from builtins is generally a bad idea, and this kind of
quirky result is one of the main reasons why.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list