[Python-3000] py3k-struni: proposition to fix ctypes bug, ctypes c_char creates bytes

Guido van Rossum guido at python.org
Wed Aug 8 20:40:52 CEST 2007


On 8/8/07, Thomas Heller <theller at ctypes.org> wrote:
> OTOH, I'm a little bit confused about the bytes type.  I think this behaviour
> is a little bit confusing, but maybe that's just me:
>
> >>> b"abc"[:]
> b'abc'
> >>> b"abc"[:1]
> b'a'
> >>> b"abc"[1]
> 98
> >>> b"abc"[1] = 42
> >>> b"abc"[1] = "f"
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'str' object cannot be interpreted as an integer
> >>> b"abc"[1] = b"f"
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'bytes' object cannot be interpreted as an integer
> >>>
>
> Especially confusing is that the repr of a bytes object looks like a string,
> but bytes do not contain characters but integers instead.

I hope you can get used to it. This design is a bit of a compromise --
conceptually, bytes really contain small unsigned integers in [0,
256), but in order to be maximally useful, the bytes literal (and
hence the bytes repr()) shows printable ASCII characters as themselves
(controls and non-ASCII are shown as \xXX). This is more compact too.
PBP!

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list