[Python-ideas] duck typing for io write methods

Nick Coghlan ncoghlan at gmail.com
Fri Jun 14 11:30:43 CEST 2013


On 14 Jun 2013 19:23, "Wolfgang Maier" <
wolfgang.maier at biologie.uni-freiburg.de> wrote:
>
> Wolfgang Maier <wolfgang.maier at ...> writes:
>
> >
> > Steven D'Aprano <steve <at> ...> writes:
> >
> > > b'\x00\x04'
> > > b'\x04\x00'
> > > b'\x00\x00\x00\x04'
> > > b'\x04\x00\x00\x00'
> > > b'\x00\x00\x00\x00\x00\x00\x00\x04'
> > > b'\x04\x00\x00\x00\x00\x00\x00\x00'
> > >
> >
> > these would be ways to make bytes([seq of ints]) work with numbers >
255,
> > which is currently not possible. Maybe bytes([seq of ints]) could take
an
> > additional encoding argument that specifies how many bytes to reserve
per int.
> >
>
> Chris Rebert just pointed out the new Python 3.2 int methods .to_bytes and
> .from_bytes, which do exactly this. Does anybody know, why .from_bytes was
> implemented as an int method instead of a bytes method .to_int ??

Because it accepts arbitrary buffer exporters (bytearray, array.array,
memoryview, mmap, ctypes, ndarray, etc), not just bytes.

(You can't write buffer exporters in pure Python at this point - there's an
open issue for that somewhere).

Cheers,
Nick.

> Wolfgang
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130614/4f91c0a7/attachment-0001.html>


More information about the Python-ideas mailing list