<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使用\_\_future\_\_ Python的每個新版本都會增加一些新的功能,或者對原來的功能作一些改動。有些改動是不兼容舊版本的,也就是在當前版本運行正常的代碼,到下一個版本運行就可能不正常了。 從Python 2.7到Python 3.x就有不兼容的一些改動,比如2.x里的字符串用`'xxx'`表示str,Unicode字符串用`u'xxx'`表示unicode,而在3.x中,所有字符串都被視為unicode,因此,寫`u'xxx'`和`'xxx'`是完全一致的,而在2.x中以`'xxx'`表示的str就必須寫成`b'xxx'`,以此表示“二進制字符串”。 要直接把代碼升級到3.x是比較冒進的,因為有大量的改動需要測試。相反,可以在2.7版本中先在一部分代碼中測試一些3.x的特性,如果沒有問題,再移植到3.x不遲。 Python提供了`__future__`模塊,把下一個新版本的特性導入到當前版本,于是我們就可以在當前版本中測試一些新版本的特性。舉例說明如下: 為了適應Python 3.x的新的字符串的表示方法,在2.7版本的代碼中,可以通過`unicode_literals`來使用Python 3.x的新的語法: ``` # still running on Python 2.7 from __future__ import unicode_literals print '\'xxx\' is unicode?', isinstance('xxx', unicode) print 'u\'xxx\' is unicode?', isinstance(u'xxx', unicode) print '\'xxx\' is str?', isinstance('xxx', str) print 'b\'xxx\' is str?', isinstance(b'xxx', str) ``` 注意到上面的代碼仍然在Python 2.7下運行,但結果顯示去掉前綴`u`的`'a string'`仍是一個unicode,而加上前綴`b`的`b'a string'`才變成了str: ``` $ python task.py 'xxx' is unicode? True u'xxx' is unicode? True 'xxx' is str? False b'xxx' is str? True ``` 類似的情況還有除法運算。在Python 2.x中,對于除法有兩種情況,如果是整數相除,結果仍是整數,余數會被扔掉,這種除法叫“地板除”: ``` >>> 10 / 3 3 ``` 要做精確除法,必須把其中一個數變成浮點數: ``` >>> 10.0 / 3 3.3333333333333335 ``` 而在Python 3.x中,所有的除法都是精確除法,地板除用`//`表示: ``` $ python3 Python 3.3.2 (default, Jan 22 2014, 09:54:40) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 10 / 3 3.3333333333333335 >>> 10 // 3 3 ``` 如果你想在Python 2.7的代碼中直接使用Python 3.x的除法,可以通過`__future__`模塊的`division`實現: ``` from __future__ import division print '10 / 3 =', 10 / 3 print '10.0 / 3 =', 10.0 / 3 print '10 // 3 =', 10 // 3 ``` 結果如下: ``` 10 / 3 = 3.33333333333 10.0 / 3 = 3.33333333333 10 // 3 = 3 ``` ### 小結 由于Python是由社區推動的開源并且免費的開發語言,不受商業公司控制,因此,Python的改進往往比較激進,不兼容的情況時有發生。Python為了確保你能順利過渡到新版本,特別提供了`__future__`模塊,讓你在舊的版本中試驗新版本的一些特性。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看