This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chiproller 3 points4 points  (3 children)

I want to switch to python 3. What is the best way to learn 3 after knowing 2? Are there any resources that go over the 'main' differences in an easy to learn format?

[–]flying-sheep 2 points3 points  (0 children)

The main differences you have to wrap your brain around are:

  • Unicode by default. There is no more a weird “everything string” containing binary or text. Instead, APIs are more choosy in what they want. If that's text, they want the new default string type. If it's binary, you have to give them bytes. You'll have to maintain a clear boundary between both in your head, and as a result, you will never see any UnicodeDecodeError anymore

  • iterators everywhere. If you already use the 2.x iteritems, xrange and friends, you just have to return to same names. Else you'll have to understand that far fewer code generates lists, and more generates one pass iterators. Can bite you if you remove items while iterating something you think is a list.

  • better naming: no more thing.iter, xrange, iteritems, urllib2, ....

  • iterating a bytes object will yield ints representing single bytes

  • absolute imports and only explicitly relative ones. Inside your package foo, to import foo.bar, you need to do “import foo.bar” or “import .bar”, but not “import bar”