<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                >[success] # 修改實例的字符串表示 ~~~ 1.讓一個實例打印出我們想要的格式展示 2.其中__repr__ 是偏向類的字符串展示 3.__str__是偏向對象的字符串展示 4.__repr__和__str__ 同時在類里面定義,print 輸出格式會按照str的來 5.如果想讓按照__repr__需要指定格式例如:'{!r}'.format(p) 用!r的格式 6.最好推薦只定義__repr__,為啥我也不知道 ~~~ >[danger] ##### 代碼說明 ~~~ 1.可以看出直接調用對象和打印對象的格式是有區別的 ~~~ ~~~ class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Test() >>> t <__main__.Test at 0x7fa91c307190> >>> print (t) <__main__.Test object at 0x7fa91c307190> ~~~ ~~~ 1.重構__repr__方法后,不管直接輸出對象還是通過print打印的信息都按我們__repr__方法中定義的格式進行顯示了 ~~~ ~~~ # 重構__repr__ class TestRepr(Test): def __repr__(self): return 'TestRepr(%s)' % self.data >>> tr = TestRepr() >>> tr TestRepr(hello, world!) >>> print tr TestRepr(hello, world!) ~~~ ~~~ 1. 你會發現,直接輸出對象ts時并沒有按我們__str__方法中定義的格式進行輸出,而用print輸出的信息卻改變了 ~~~ ~~~ # 重構__str__ calss TestStr(Test): def __str__(self): return '[Value: %s]' % self.data >>> ts = TestStr() >>> ts <__main__.TestStr at 0x7fa91c314e50> >>> print (ts) [Value: hello, world!] ~~~ ~~~ 1.當同時定義的時候print輸出的結果是定義的str 2.當想通過print打印出repr 給利用指定格式!r ~~~ ~~~ # 同時定義 class Pair: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return 'Pair({0.x!r}, {0.y!r})'.format(self) def __str__(self): return '1' p = Pair(3,4) print(p) print('{!r}'.format(p)) 打印結果: 1 Pair(3, 4) ~~~
                  <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>

                              哎呀哎呀视频在线观看