[Python-ideas] duck typing for io write methods
MRAB
python at mrabarnett.plus.com
Sun Jun 16 22:58:36 CEST 2013
On 16/06/2013 21:51, Wolfgang Maier wrote:
> Benjamin Peterson <benjamin at ...> writes:
>
>>
>> Wolfgang Maier <wolfgang.maier <at> ...> writes:
>>
>> > However, if you decide to inherit from str or int, then bytes() completely
>> > ignores the __bytes__ method and sticks to the superclass behavior instead,
>> > i.e. requiring an encoding for str and creating a bytestring of the length
>> > of an int.
>>
>> int is fixed in 3.3.
>>
>
> Ah right, I found this now as Issue17309 at bugs.python.org, and you're
> saying there that it's been fixed. I just tested int with Python3.3.0 on
> Windows and it still ignores __bytes__. Which exact version are you
> referring to?
> class bint (int):
> def __bytes__(self):
> return b'now bytes'
> a=bint(4)
> bytes(a)
> -> b'\x00\x00\x00\x00'
>
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32
bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> class bint (int):
def __bytes__(self):
return b'now bytes'
>>> a=bint(4)
>>> bytes(a)
b'now bytes'
More information about the Python-ideas
mailing list