<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國際加速解決方案。 廣告
                # Python `bin()`函數 > 原文: [https://thepythonguru.com/python-builtin-functions/bin/](https://thepythonguru.com/python-builtin-functions/bin/) * * * 于 2020 年 1 月 7 日更新 * * * `bin()`函數以字符串形式返回整數的二進制表示形式。 其語法如下: ```py bin(number) -> binary representation ``` | 參數 | 描述 | | --- | --- | | `number` | 任何數值 | 這是一個例子: ```py >>> >>> bin(4) # convert decimal to binary '0b100' >>> >>> >>> bin(0xff) # convert hexadecimal to binary, 0xff is same decimal 255 '0b11111111' >>> >>> >>> bin(0o24) # convert octacl to binary, 0o24 is same decimal 20 '0b10100' >>> ``` ```py print(bin(4)) print(bin(0xff)) print(bin(0o24)) ``` ## `bin()`與用戶定義的對象 * * * 要將`bin()`與用戶定義的對象一起使用,我們必須首先重載`__index__()`方法。 在切片和索引的上下文中,`__index__()`方法用于將對象強制為整數。 例如,考慮以下內容: ```py >>> >>> l = [1, 2, 3, 4, 5] >>> >>> x, y = 1, 3 >>> >>> >>> l[x] 2 >>> >>> >>> l[y] 4 >>> >>> >>> l[x:y] [2, 3] >>> ``` ```py l = [1, 2, 3, 4, 5] x, y = 1, 3 print(l[x]) print(l[y]) print(l[x:y]) ``` 當我們使用索引和切片訪問列表中的項目時,內部 Python 會調用`int`對象的`__index__()`方法。 ```py >>> >>> l[x.__index__()] # same as l[x] 2 >>> >>> >>> l[y.__index__()] # same as l[y] 4 >>> >>> >>> l[x.__index__():y.__index__()] # # same as l[x:y] [2, 3] >>> ``` ```py l = [1, 2, 3, 4, 5] x, y = 1, 3 print(l[x.__index__()]) print(l[y.__index__()]) print(l[x.__index__():y.__index__()]) ``` 除了`bin()`之外,在對象上調用`hex()`和`oct()`時也會調用`__index__()`方法。 這是一個例子: ```py >>> >>> class Num: ... def __index__(self): ... return 4 ... >>> >>> l = [1, 2, 3, 4, 5] >>> >>> >>> n1 = Num() >>> >>> >>> bin(n1) 0b100 >>> >>> >>> hex(n1) 0x4 >>> >>> >>> oct(n1) 0o4 >>> >>> >>> l[n1] 5 >>> >>> >>> l[n1.__index__()] 5 >>> ``` ```py class Num: def __index__(self): return 4 l = [1, 2, 3, 4, 5] n1 = Num() print(bin(n1)) print(hex(n1)) print(oct(n1)) print(l[n1]) print(l[n1.__index__()]) ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看