News for hackers. Stuff that matters.
Re:Why Python 3 Exists by cjauvin on Thursday December 17, 2015 @11:54AM (#10752575)
I know it's a deeply subjective and personal thing unlikely to help anyone other than me, but I've always successfully relied on the following mnemonic for this particular problem:

* unicode -> encode, u -> e (vowels)

* str -> decode, s -> d (consonants)

Re:Why Python 3 Exists by daveguy on Thursday December 17, 2015 @12:09PM (#10752672)
My way of remembering str -> decode; (s->d); sad. sad that I have to remember this and that it is still the "future of the language" despite significant and ongoing resistance.

EDIT: This last line of the article pretty much sums it up: "We structured the transition thinking the community would come along with us in leaving Python 2 behind, but that turned out not to be the case and instead we have taken some more time and are using a Python 2/3 compatible subset of the language to manage the transition."

I wonder if python 3.6 or 3.7 will acquiesce to this and give an option for something like "from __past__ import str" like we have for __future__ in 2.7 to make these backward incompatibilities easier to deal with.

Re:Why Python 3 Exists by the_mitsuhiko on Thursday December 17, 2015 @01:50PM (#10753496)
> from __past__ import str

As the past did not contain the __past__ that is not going to work.

Re:Why Python 3 Exists by daveguy on Thursday December 17, 2015 @02:11PM (#10753672)
Excellent point. I guess the shims that collinmanderson references would be the ideal.
Re:Why Python 3 Exists by collinmanderson on Thursday December 17, 2015 @02:00PM (#10753599)
It would be nice if future python 3.x brought back shims for unicode(), iteritems, iterkeys, itervalues, xrange(), etc. That could help make the transition easier.
Re:Why Python 3 Exists by s73v3r on Thursday December 17, 2015 @01:10PM (#10753145)
Why? What possible reason would there be to use the old behavior, except for laziness?
Re:Why Python 3 Exists by squeaky-clean on Thursday December 17, 2015 @02:45PM (#10753943)
Because I'd like to be able to use the googleads Python client library within Python 3 without needing to fix 62,000 lines of code? I guess you could call that laziness if you want.
Re:Why Python 3 Exists by takeda on Thursday December 17, 2015 @09:43PM (#10756284)
You just simply use it, if specific argument require python 2 sting you simply call bytes() on it.

For example

old_function(bytes(myarg))

if it needs to be a literal:

old_function(b"literal")

That's what I do, and works for me so far.

Re:Why Python 3 Exists by daveguy on Thursday December 17, 2015 @01:14PM (#10753171)
Nice. Thanks for attributing a negative personality trait as the only reason someone would prefer any other way.
Re:Why Python 3 Exists by chc on Thursday December 17, 2015 @01:40PM (#10753396)
I don't know if it's been edited since you posted this, but at the moment, the comment is a question asking why somebody would want this other than laziness.
Re:Why Python 3 Exists by daveguy on Thursday December 17, 2015 @02:09PM (#10753657)
True. I guess some would consider this a positive personality trait in a programmer. I think that's only for system design though (lazy for minimizing future effort required).
Re:Why Python 3 Exists by andrewstuart on Thursday December 17, 2015 @11:57AM (#10752598)
There should be one obvious way to do it.
Re:Why Python 3 Exists by davidism on Thursday December 17, 2015 @12:02PM (#10752625)
> Although that way may not be obvious at first unless you're Dutch.

There is one obvious way: strings are encoded to bytes, bytes are decoded to strings.

Re:Why Python 3 Exists by andrewstuart on Thursday December 17, 2015 @12:08PM (#10752664)
I don't think it is obvious and intuitive and unambiguous what the difference is between ENcoding and DEcoding.
Re:Why Python 3 Exists by Veedrac on Thursday December 17, 2015 @12:24PM (#10752780)
A string is an abstract bit of text. You need to encode this into a particular memory representation of the text.

Bytes hold a bunch of data in some encoding. It could be an image, UTF-8 or LZMA compressed ASCII. Once you know the encoding, to reconstruct the data you decode into a semantically meaningful form.

To put it another way, imagine the terms were "serialize" and "deserialize". Of course one serializes to and deserializes from binary data. Just replace "{,de}serialize" with "{en,de}code" and you're done.

Re:Why Python 3 Exists by lawpoop on Thursday December 17, 2015 @01:08PM (#10753126)
You mean the prefixes 'en-' and 'de-'?
Re:Why Python 3 Exists by odonnellryan on Thursday December 17, 2015 @01:35PM (#10753350)
Encode and Decode are slightly subjective... why not something like to_bytes and from_bytes? Maybe not the best names, but definitely clearer on the meaning.
Re:Why Python 3 Exists by takeda on Thursday December 17, 2015 @09:50PM (#10756306)
Not really.

Veedrac had a good analogy, think of text as something abstract, for example imagine text is an image or sound, if you want to store it in bytes you need to encode it, and to read back you decode it.

As to_bytes/from_bytes, actually python provides it too:

to_bytes -> bytes(<text>)

from_bytes -> str(<bytes>)

Re:Why Python 3 Exists by odonnellryan on Thursday December 17, 2015 @10:53PM (#10756518)
It makes sense for sure, just isn't super intuitive - if it was people wouldn't be so confused.
Re:Why Python 3 Exists by jes5199 on Thursday December 17, 2015 @12:06PM (#10752656)
I think that's backwards
Re:Why Python 3 Exists by rspeer on Thursday December 17, 2015 @12:46PM (#10752968)
It's not backwards.

I think that reveals that the names really do have a problem. The problem is that "encode" sounds like "make this Unicode" to people who aren't familiar with Unicode.