TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Differences between Python 2.7.x and Python 3.x, with examples

108 pointsby rasbtalmost 11 years ago

7 comments

ak217almost 11 years ago
&quot;Now, in Python 3, we finally have Unicode (utf-8) strings, and 2 byte classes: byte and bytearrays.&quot;<p>No, they are Unicode strings. utf-8 is an encoding, and only comes into play when you want to encode strings into bytes for sending them somewhere, or decode them from bytes when receiving them. The interpreter&#x27;s internal representation of the string is either UCS-4 or an automatically selected encoding (<a href="http://legacy.python.org/dev/peps/pep-0393/" rel="nofollow">http:&#x2F;&#x2F;legacy.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0393&#x2F;</a>), but that is an irrelevant implementation detail. Conceptually, the strings are sequences of Unicode characters, and it helps to think of them that way.<p>Here are the really important facts about Unicode handling differences between Python 2 and 3 (aside from the obvious str&#x2F;unicode -&gt; bytes&#x2F;str move):<p>- There is no silent Unicode coercion in Python 3. Unlike Python 2, your bytes objects won&#x27;t be decoded for you to str just because you happened to concatenate them with a Unicode string. Your Unicode strings won&#x27;t be encoded silently if you write them to a byte stream (instead, Python 3 will fail with the cryptic error &quot;TypeError: &#x27;str&#x27; does not support the buffer interface&quot;).<p>- The default encoding in Python 3 is utf-8, instead of the insane ascii default in Python 2.<p>- All text I&#x2F;O methods by default return decoded strings, except if you open a stream in binary mode (open(filename, &quot;b&quot;)), which now actually means what you&#x27;d expect. See the documentation for the io module (<a href="https://docs.python.org/3.4/library/io.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3.4&#x2F;library&#x2F;io.html</a>) for more information. (You can use the io module in Python 2.7 to write code that is more forward-compatible with Python 3.)<p>- The above I&#x2F;O semantics includes sys.argv, os.environ, and the standard streams (sys.stdin&#x2F;stdout&#x2F;stderr). The fact that all of these behave differently between Python 2 and 3 with respect to text encoding makes for a lot of fun hair pulling when trying to write code compatible with both.<p>I have built a small library of helper functions to help deal with this stuff in a sane way: <a href="https://github.com/kislyuk/eight" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kislyuk&#x2F;eight</a>. Another project that I can recommend that tries to lessen the pain of writing code that&#x27;s compatible with both 2 and 3 is python-future: <a href="https://github.com/PythonCharmers/python-future" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;PythonCharmers&#x2F;python-future</a>.
评论 #8027290 未加载
评论 #8026709 未加载
评论 #8027132 未加载
pdknskalmost 11 years ago
There are other, more subtle differences. In Python 3 this doesn&#x27;t work.<p><pre><code> &gt;&gt;&gt; filter(lambda (x, y): x &gt; y, ((1, 2), (4, 3))) </code></pre> And 2.7 returns the filter result in the input type.<p><pre><code> &gt;&gt;&gt; filter(lambda x: x in &#x27;ABC&#x27;, &#x27;ABCDEFA&#x27;) &#x27;ABCA&#x27; </code></pre> In 3 it&#x27;s an extra step.<p><pre><code> &gt;&gt;&gt; &#x27;&#x27;.join(filter(lambda x: x in &#x27;ABC&#x27;, &#x27;ABCDEFA&#x27;)) &#x27;ABCA&#x27;</code></pre>
tzuryalmost 11 years ago
The `xrange` example suggests that in Python 3.x, `range `(which is equivalent to xrange in python 2.x is slower than range in python 2.x.<p>This is a double patently IMHO.<p><a href="http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html#xrange" rel="nofollow">http:&#x2F;&#x2F;sebastianraschka.com&#x2F;Articles&#x2F;2014_python_2_3_key_dif...</a>
rdtscalmost 11 years ago
Vis-a-vis the unicode issue this is another post (by Armin Ronacher&#x2F;mitsuhiko, creator of Flask web framework)<p><a href="http://lucumr.pocoo.org/2014/5/12/everything-about-unicode/" rel="nofollow">http:&#x2F;&#x2F;lucumr.pocoo.org&#x2F;2014&#x2F;5&#x2F;12&#x2F;everything-about-unicode&#x2F;</a>
wtingalmost 11 years ago
&gt; However, it is also possible - in contrast to generators - to iterate over those multiple times if needed, it is aonly not so efficient.<p>You can reuse a generator multiple times via itertools.tee():<p><a href="https://docs.python.org/2/library/itertools.html#itertools.tee" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;itertools.html#itertools.t...</a>
thomkalmost 11 years ago
Typo in this sentence: However, it is also possible - in contrast to generators - to iterate over those multiple times if needed, it is aonly not so efficient.
gomesnayagamalmost 11 years ago
thought many improvement happened, developer still prefer to use 2.x and the eco system take long time to adopt 3.x. &quot;who tie the bell to the cat&quot;