Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Chris Angelico
On Wed, May 16, 2018 at 9:02 AM, Skip Montanaro
 wrote:
>> Personally, I never use the str(..., encoding="...") notation; I prefer
> to use the
>> .decode() method of the bytes object.
>
> Thanks. And thanks for the other responses. I obviously didn't have my
> thinking cap on this morning. (It was too late in the morning to plead lack
> of coffee.)

You plead lack of coffee until you plead caffeine overdose. That's the
way of things. :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Skip Montanaro
> Personally, I never use the str(..., encoding="...") notation; I prefer
to use the
> .decode() method of the bytes object.

Thanks. And thanks for the other responses. I obviously didn't have my
thinking cap on this morning. (It was too late in the morning to plead lack
of coffee.)

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Chris Angelico
On Wed, May 16, 2018 at 1:14 AM, Skip Montanaro
 wrote:
> Consider this:
>
 bytes("abc", encoding="utf-8")
> b'abc'
>
> Looks reasonable. Then consider this:
>
 str(bytes("abc", encoding="utf-8"))
> "b'abc'"
>
> Why is the b'...' bit still there? I suppose it's because I didn't tell it
> explicitly how to decode the bytes object, as when I do, I get the expected
> result:
>
 str(bytes("abc", encoding="utf-8"), encoding="utf-8")
> 'abc'
>
> Coming from a still largely Python 2 perspective, did all attempts to apply
> default encodings disappear in Python 3?

It's there for the same reason as the square brackets here:

>>> str(list('abc'))
"['a', 'b', 'c']"

Calling str() on arbitrary objects returns a printable string; and in
Py3, there's simply nothing special about bytes. Personally, I never
use the str(..., encoding="...") notation; I prefer to use the
.decode() method of the bytes object.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Serhiy Storchaka

15.05.18 18:14, Skip Montanaro пише:

Consider this:


bytes("abc", encoding="utf-8")

b'abc'

Looks reasonable. Then consider this:


str(bytes("abc", encoding="utf-8"))

"b'abc'"

Why is the b'...' bit still there? I suppose it's because I didn't tell it
explicitly how to decode the bytes object, as when I do, I get the expected
result:


str(bytes("abc", encoding="utf-8"), encoding="utf-8")

'abc'


str() without 'encoding' and 'errors' calls __str__ which falls back to 
__repr__ by default.


bytes.__str__ falls back to bytes.__repr__, this makes errors of mixing 
bytes and strings more obvious. If run Python with the -b option, it 
will emit a BytesWarning first. If run it with -bb, it will become an error.


You can receive many interesting warning when run Python 2 with options 
-b and -3. Getting rid of these warnings will help with porting to 
Python 3 and may fix real bugs.


--
https://mail.python.org/mailman/listinfo/python-list


Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Ben Finney
Skip Montanaro  writes:

> Consider this:
>
> >>> bytes("abc", encoding="utf-8")
> b'abc'
>
> Looks reasonable. Then consider this:
>
> >>> str(bytes("abc", encoding="utf-8"))
> "b'abc'"
>
> Why is the b'...' bit still there?

Because the bytes object is asked for a text representation of itself,
and the text value ‘b'abc'’ is what it returned.

> I suppose it's because I didn't tell it explicitly how to decode the
> bytes object, as when I do, I get the expected result:
>
> >>> str(bytes("abc", encoding="utf-8"), encoding="utf-8")
> 'abc'

Yes.

> Coming from a still largely Python 2 perspective, did all attempts to
> apply default encodings disappear in Python 3?

To the extent I understand that question, the answer is no.

Rather, the ‘bytes’ and ‘str’ types are now entirely incompatible, and
implicit conversions are never done between them. Any conversions
between them must be explicit.

-- 
 \“There are always those who think they know what is your |
  `\  responsibility better than you do.” —Ralph Waldo Emerson |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Ethan Furman

On 05/15/2018 08:14 AM, Skip Montanaro wrote:

Consider this:


bytes("abc", encoding="utf-8")

b'abc'

Looks reasonable. Then consider this:


str(bytes("abc", encoding="utf-8"))

"b'abc'"

Why is the b'...' bit still there?


Because you are printing a bytes object, not a str.


I suppose it's because I didn't tell it
explicitly how to decode the bytes object, as when I do, I get the expected
result:


str(bytes("abc", encoding="utf-8"), encoding="utf-8")

'abc'

Coming from a still largely Python 2 perspective, did all attempts to apply
default encodings disappear in Python 3?


Pretty much, yup.  There is no more guessing what encoding to use -- either specify it, or be made aware that you 
printed a bytes object.


--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


What's the rationale for b"..." in this example?

2018-05-15 Thread Skip Montanaro
Consider this:

>>> bytes("abc", encoding="utf-8")
b'abc'

Looks reasonable. Then consider this:

>>> str(bytes("abc", encoding="utf-8"))
"b'abc'"

Why is the b'...' bit still there? I suppose it's because I didn't tell it
explicitly how to decode the bytes object, as when I do, I get the expected
result:

>>> str(bytes("abc", encoding="utf-8"), encoding="utf-8")
'abc'

Coming from a still largely Python 2 perspective, did all attempts to apply
default encodings disappear in Python 3?

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list