[Python-ideas] duck typing for io write methods

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Jun 13 16:41:34 CEST 2013


Nick Coghlan <ncoghlan at ...> writes:

> 
> > Oscar Benjamin <oscar.j.benjamin <at> ...> writes:
> >>
> >> On 13 June 2013 13:24, Nick Coghlan <ncoghlan <at> ...> wrote:
> >> > If your type is acceptable input to operator.index(), you'll get the
> >> > "initialised array of bytes" behaviour
> >>
> >> I only recently discovered this. What was the rationale for that change?
> >>
> >> $ py -2.7 -c 'print(repr(bytes(4)))'
> >> '4'
> >>
> >> $ py -3.3 -c 'print(repr(bytes(4)))'
> >> b'\x00\x00\x00\x00'
> >>
> >> I can't really see why anyone would want the latter behaviour (when
> >> you can already do b'\x00' * 4).
> >>
> >> Oscar
> >>
> >
> > It's funny you mention that difference since that was how I came across my
> > issue. I was looking for a way to get back the Python 2.7 behaviour
> > bytes('1234')
> > '1234'
> 
> You mean other than using the bytes literal b'1234' instead of a
> string literal? Bytes and text are different things in Python 3,
> whereas the 2.x "bytes" was just an alias for "str".
>
 
Well, I was illustrating the case with a literal integer, but, of course, I
was thinking of cases with references:
a=1234
str(a).encode() # gives b'1234' in Python3, but converting your int to str
first, just to encode it again to bytes seems weird
Best,
Wolfgang



More information about the Python-ideas mailing list